utils.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* *INDENT-OFF* */ /* ATTRIBUTE_PRINTF confuses indent, avoid running it
  2. for now. */
  3. /* I/O, string, cleanup, and other random utilities for GDB.
  4. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  5. This file is part of GDB.
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifndef UTILS_H
  17. #define UTILS_H
  18. #include "exceptions.h"
  19. #include "gdbsupport/array-view.h"
  20. #include "gdbsupport/scoped_restore.h"
  21. #include <chrono>
  22. #ifdef HAVE_LIBXXHASH
  23. #include <xxhash.h>
  24. #endif
  25. struct completion_match_for_lcd;
  26. class compiled_regex;
  27. /* String utilities. */
  28. extern bool sevenbit_strings;
  29. /* Modes of operation for strncmp_iw_with_mode. */
  30. enum class strncmp_iw_mode
  31. {
  32. /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
  33. differences in whitespace. Returns 0 if they match, non-zero if
  34. they don't (slightly different than strcmp()'s range of return
  35. values). */
  36. NORMAL,
  37. /* Like NORMAL, but also apply the strcmp_iw hack. I.e.,
  38. string1=="FOO(PARAMS)" matches string2=="FOO". */
  39. MATCH_PARAMS,
  40. };
  41. /* Helper for strcmp_iw and strncmp_iw. Exported so that languages
  42. can implement both NORMAL and MATCH_PARAMS variants in a single
  43. function and defer part of the work to strncmp_iw_with_mode.
  44. LANGUAGE is used to implement some context-sensitive
  45. language-specific comparisons. For example, for C++,
  46. "string1=operator()" should not match "string2=operator" even in
  47. MATCH_PARAMS mode.
  48. MATCH_FOR_LCD is passed down so that the function can mark parts of
  49. the symbol name as ignored for completion matching purposes (e.g.,
  50. to handle abi tags). If IGNORE_TEMPLATE_PARAMS is true, all template
  51. parameter lists will be ignored when language is C++. */
  52. extern int strncmp_iw_with_mode
  53. (const char *string1, const char *string2, size_t string2_len,
  54. strncmp_iw_mode mode, enum language language,
  55. completion_match_for_lcd *match_for_lcd = NULL,
  56. bool ignore_template_params = false);
  57. /* Do a strncmp() type operation on STRING1 and STRING2, ignoring any
  58. differences in whitespace. STRING2_LEN is STRING2's length.
  59. Returns 0 if STRING1 matches STRING2_LEN characters of STRING2,
  60. non-zero otherwise (slightly different than strncmp()'s range of
  61. return values). Note: passes language_minimal to
  62. strncmp_iw_with_mode, and should therefore be avoided if a more
  63. suitable language is available. */
  64. extern int strncmp_iw (const char *string1, const char *string2,
  65. size_t string2_len);
  66. /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
  67. differences in whitespace. Returns 0 if they match, non-zero if
  68. they don't (slightly different than strcmp()'s range of return
  69. values).
  70. As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
  71. This "feature" is useful when searching for matching C++ function
  72. names (such as if the user types 'break FOO', where FOO is a
  73. mangled C++ function).
  74. Note: passes language_minimal to strncmp_iw_with_mode, and should
  75. therefore be avoided if a more suitable language is available. */
  76. extern int strcmp_iw (const char *string1, const char *string2);
  77. extern int strcmp_iw_ordered (const char *, const char *);
  78. /* Return true if the strings are equal. */
  79. extern bool streq (const char *, const char *);
  80. extern int subset_compare (const char *, const char *);
  81. /* Compare C strings for std::sort. */
  82. static inline bool
  83. compare_cstrings (const char *str1, const char *str2)
  84. {
  85. return strcmp (str1, str2) < 0;
  86. }
  87. /* Reset the prompt_for_continue clock. */
  88. void reset_prompt_for_continue_wait_time (void);
  89. /* Return the time spent in prompt_for_continue. */
  90. std::chrono::steady_clock::duration get_prompt_for_continue_wait_time ();
  91. /* Parsing utilities. */
  92. extern int parse_pid_to_attach (const char *args);
  93. extern int parse_escape (struct gdbarch *, const char **);
  94. /* Cleanup utilities. */
  95. extern void init_page_info (void);
  96. /* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
  97. Restore when destroyed. */
  98. struct set_batch_flag_and_restore_page_info
  99. {
  100. public:
  101. set_batch_flag_and_restore_page_info ();
  102. ~set_batch_flag_and_restore_page_info ();
  103. DISABLE_COPY_AND_ASSIGN (set_batch_flag_and_restore_page_info);
  104. private:
  105. /* Note that this doesn't use scoped_restore, because it's important
  106. to control the ordering of operations in the destruction, and it
  107. was simpler to avoid introducing a new ad hoc class. */
  108. unsigned m_save_lines_per_page;
  109. unsigned m_save_chars_per_line;
  110. int m_save_batch_flag;
  111. };
  112. /* Path utilities. */
  113. extern int gdb_filename_fnmatch (const char *pattern, const char *string,
  114. int flags);
  115. extern void substitute_path_component (char **stringp, const char *from,
  116. const char *to);
  117. std::string ldirname (const char *filename);
  118. extern int count_path_elements (const char *path);
  119. extern const char *strip_leading_path_elements (const char *path, int n);
  120. /* GDB output, ui_file utilities. */
  121. struct ui_file;
  122. extern int query (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  123. extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  124. extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  125. extern void begin_line (void);
  126. extern void wrap_here (int);
  127. extern void reinitialize_more_filter (void);
  128. /* Return the number of characters in a line. */
  129. extern int get_chars_per_line ();
  130. extern bool pagination_enabled;
  131. /* A flag indicating whether to timestamp debugging messages. */
  132. extern bool debug_timestamp;
  133. extern struct ui_file **current_ui_gdb_stdout_ptr (void);
  134. extern struct ui_file **current_ui_gdb_stdin_ptr (void);
  135. extern struct ui_file **current_ui_gdb_stderr_ptr (void);
  136. extern struct ui_file **current_ui_gdb_stdlog_ptr (void);
  137. /* Flush STREAM. */
  138. extern void gdb_flush (struct ui_file *stream);
  139. /* The current top level's ui_file streams. */
  140. /* Normal results */
  141. #define gdb_stdout (*current_ui_gdb_stdout_ptr ())
  142. /* Input stream */
  143. #define gdb_stdin (*current_ui_gdb_stdin_ptr ())
  144. /* Serious error notifications. This bypasses the pager, if one is in
  145. use. */
  146. #define gdb_stderr (*current_ui_gdb_stderr_ptr ())
  147. /* Log/debug/trace messages that bypasses the pager, if one is in
  148. use. */
  149. #define gdb_stdlog (*current_ui_gdb_stdlog_ptr ())
  150. /* Truly global ui_file streams. These are all defined in main.c. */
  151. /* Target output that should bypass the pager, if one is in use. */
  152. extern struct ui_file *gdb_stdtarg;
  153. extern struct ui_file *gdb_stdtargerr;
  154. extern struct ui_file *gdb_stdtargin;
  155. /* Set the screen dimensions to WIDTH and HEIGHT. */
  156. extern void set_screen_width_and_height (int width, int height);
  157. /* Generic stdio-like operations. */
  158. extern void gdb_puts (const char *, struct ui_file *);
  159. extern int gdb_putc (int c, struct ui_file *);
  160. extern int gdb_putc (int c);
  161. extern void gdb_puts (const char *);
  162. extern void puts_tabular (char *string, int width, int right);
  163. /* Generic printf-like operations. As an extension over plain
  164. printf, these support some GDB-specific format specifiers.
  165. Particularly useful here are the styling formatters: '%p[', '%p]'
  166. and '%ps'. See ui_out::message for details. */
  167. extern void gdb_vprintf (const char *, va_list) ATTRIBUTE_PRINTF (1, 0);
  168. extern void gdb_vprintf (struct ui_file *, const char *, va_list)
  169. ATTRIBUTE_PRINTF (2, 0);
  170. extern void gdb_printf (struct ui_file *, const char *, ...)
  171. ATTRIBUTE_PRINTF (2, 3);
  172. extern void gdb_printf (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  173. extern void printf_unfiltered (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
  174. extern void print_spaces (int, struct ui_file *);
  175. extern const char *n_spaces (int);
  176. /* Return nonzero if filtered printing is initialized. */
  177. extern int filtered_printing_initialized (void);
  178. /* Like gdb_printf, but styles the output according to STYLE,
  179. when appropriate. */
  180. extern void fprintf_styled (struct ui_file *stream,
  181. const ui_file_style &style,
  182. const char *fmt,
  183. ...)
  184. ATTRIBUTE_PRINTF (3, 4);
  185. /* Like gdb_puts, but styles the output according to STYLE, when
  186. appropriate. */
  187. extern void fputs_styled (const char *linebuffer,
  188. const ui_file_style &style,
  189. struct ui_file *stream);
  190. /* Like fputs_styled, but uses highlight_style to highlight the
  191. parts of STR that match HIGHLIGHT. */
  192. extern void fputs_highlighted (const char *str, const compiled_regex &highlight,
  193. struct ui_file *stream);
  194. /* Return the address only having significant bits. */
  195. extern CORE_ADDR address_significant (gdbarch *gdbarch, CORE_ADDR addr);
  196. /* Convert CORE_ADDR to string in platform-specific manner.
  197. This is usually formatted similar to 0x%lx. */
  198. extern const char *paddress (struct gdbarch *gdbarch, CORE_ADDR addr);
  199. /* Return a string representation in hexadecimal notation of ADDRESS,
  200. which is suitable for printing. */
  201. extern const char *print_core_address (struct gdbarch *gdbarch,
  202. CORE_ADDR address);
  203. extern CORE_ADDR string_to_core_addr (const char *my_string);
  204. extern void fprintf_symbol (struct ui_file *, const char *,
  205. enum language, int);
  206. extern void throw_perror_with_name (enum errors errcode, const char *string)
  207. ATTRIBUTE_NORETURN;
  208. extern void perror_warning_with_name (const char *string);
  209. extern void print_sys_errmsg (const char *, int);
  210. /* Warnings and error messages. */
  211. extern void (*deprecated_error_begin_hook) (void);
  212. /* Message to be printed before the warning message, when a warning occurs. */
  213. extern const char *warning_pre_print;
  214. extern void error_stream (const string_file &) ATTRIBUTE_NORETURN;
  215. extern void demangler_vwarning (const char *file, int line,
  216. const char *, va_list ap)
  217. ATTRIBUTE_PRINTF (3, 0);
  218. extern void demangler_warning (const char *file, int line,
  219. const char *, ...) ATTRIBUTE_PRINTF (3, 4);
  220. /* Misc. utilities. */
  221. #ifdef HAVE_WAITPID
  222. extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout);
  223. #endif
  224. extern int myread (int, char *, int);
  225. /* Integer exponentiation: Return V1**V2, where both arguments
  226. are integers.
  227. Requires V1 != 0 if V2 < 0.
  228. Returns 1 for 0 ** 0. */
  229. extern ULONGEST uinteger_pow (ULONGEST v1, LONGEST v2);
  230. /* Resource limits used by getrlimit and setrlimit. */
  231. enum resource_limit_kind
  232. {
  233. LIMIT_CUR,
  234. LIMIT_MAX
  235. };
  236. /* Check whether GDB will be able to dump core using the dump_core
  237. function. Returns zero if GDB cannot or should not dump core.
  238. If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
  239. If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
  240. extern int can_dump_core (enum resource_limit_kind limit_kind);
  241. /* Print a warning that we cannot dump core. */
  242. extern void warn_cant_dump_core (const char *reason);
  243. /* Dump core trying to increase the core soft limit to hard limit
  244. first. */
  245. extern void dump_core (void);
  246. /* Copy NBITS bits from SOURCE to DEST starting at the given bit
  247. offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
  248. Source and destination buffers must not overlap. */
  249. extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
  250. const gdb_byte *source, ULONGEST source_offset,
  251. ULONGEST nbits, int bits_big_endian);
  252. /* A fast hashing function. This can be used to hash data in a fast way
  253. when the length is known. If no fast hashing library is available, falls
  254. back to iterative_hash from libiberty. START_VALUE can be set to
  255. continue hashing from a previous value. */
  256. static inline unsigned int
  257. fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
  258. {
  259. #ifdef HAVE_LIBXXHASH
  260. return XXH64 (ptr, len, start_value);
  261. #else
  262. return iterative_hash (ptr, len, start_value);
  263. #endif
  264. }
  265. #endif /* UTILS_H */