common-utils.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Shared general utility routines for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_COMMON_UTILS_H
  15. #define COMMON_COMMON_UTILS_H
  16. #include <string>
  17. #include <vector>
  18. #include "gdbsupport/byte-vector.h"
  19. #include "gdbsupport/gdb_unique_ptr.h"
  20. #include "poison.h"
  21. #include "gdb_string_view.h"
  22. /* xmalloc(), xrealloc() and xcalloc() have already been declared in
  23. "libiberty.h". */
  24. /* Like xmalloc, but zero the memory. */
  25. void *xzalloc (size_t);
  26. /* Like asprintf and vasprintf, but return the string, throw an error
  27. if no memory. */
  28. gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...)
  29. ATTRIBUTE_PRINTF (1, 2);
  30. gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap)
  31. ATTRIBUTE_PRINTF (1, 0);
  32. /* Like snprintf, but throw an error if the output buffer is too small. */
  33. int xsnprintf (char *str, size_t size, const char *format, ...)
  34. ATTRIBUTE_PRINTF (3, 4);
  35. /* Returns a std::string built from a printf-style format string. */
  36. std::string string_printf (const char* fmt, ...)
  37. ATTRIBUTE_PRINTF (1, 2);
  38. /* Like string_printf, but takes a va_list. */
  39. std::string string_vprintf (const char* fmt, va_list args)
  40. ATTRIBUTE_PRINTF (1, 0);
  41. /* Like string_printf, but appends to DEST instead of returning a new
  42. std::string. */
  43. std::string &string_appendf (std::string &dest, const char* fmt, ...)
  44. ATTRIBUTE_PRINTF (2, 3);
  45. /* Like string_appendf, but takes a va_list. */
  46. std::string &string_vappendf (std::string &dest, const char* fmt, va_list args)
  47. ATTRIBUTE_PRINTF (2, 0);
  48. /* Make a copy of the string at PTR with LEN characters
  49. (and add a null character at the end in the copy).
  50. Uses malloc to get the space. Returns the address of the copy. */
  51. char *savestring (const char *ptr, size_t len);
  52. /* Extract the next word from ARG. The next word is defined as either,
  53. everything up to the next space, or, if the next word starts with either
  54. a single or double quote, then everything up to the closing quote. The
  55. enclosing quotes are not returned in the result string. The pointer in
  56. ARG is updated to point to the first character after the end of the
  57. word, or, for quoted words, the first character after the closing
  58. quote. */
  59. std::string extract_string_maybe_quoted (const char **arg);
  60. /* The strerror() function can return NULL for errno values that are
  61. out of range. Provide a "safe" version that always returns a
  62. printable string. This version is also thread-safe. */
  63. extern const char *safe_strerror (int);
  64. /* Version of startswith that takes string_view arguments. Return
  65. true if the start of STRING matches PATTERN, false otherwise. */
  66. static inline bool
  67. startswith (gdb::string_view string, gdb::string_view pattern)
  68. {
  69. return (string.length () >= pattern.length ()
  70. && strncmp (string.data (), pattern.data (), pattern.length ()) == 0);
  71. }
  72. ULONGEST strtoulst (const char *num, const char **trailer, int base);
  73. /* Skip leading whitespace characters in INP, returning an updated
  74. pointer. If INP is NULL, return NULL. */
  75. extern char *skip_spaces (char *inp);
  76. /* A const-correct version of the above. */
  77. extern const char *skip_spaces (const char *inp);
  78. /* Skip leading non-whitespace characters in INP, returning an updated
  79. pointer. If INP is NULL, return NULL. */
  80. extern char *skip_to_space (char *inp);
  81. /* A const-correct version of the above. */
  82. extern const char *skip_to_space (const char *inp);
  83. /* Assumes that V is an argv for a program, and iterates through
  84. freeing all the elements. */
  85. extern void free_vector_argv (std::vector<char *> &v);
  86. /* Return true if VALUE is in [LOW, HIGH]. */
  87. template <typename T>
  88. static bool
  89. in_inclusive_range (T value, T low, T high)
  90. {
  91. return value >= low && value <= high;
  92. }
  93. /* Ensure that V is aligned to an N byte boundary (N's assumed to be a
  94. power of 2). Round up/down when necessary. Examples of correct
  95. use include:
  96. addr = align_up (addr, 8); -- VALUE needs 8 byte alignment
  97. write_memory (addr, value, len);
  98. addr += len;
  99. and:
  100. sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned
  101. write_memory (sp, value, len);
  102. Note that uses such as:
  103. write_memory (addr, value, len);
  104. addr += align_up (len, 8);
  105. and:
  106. sp -= align_up (len, 8);
  107. write_memory (sp, value, len);
  108. are typically not correct as they don't ensure that the address (SP
  109. or ADDR) is correctly aligned (relying on previous alignment to
  110. keep things right). This is also why the methods are called
  111. "align_..." instead of "round_..." as the latter reads better with
  112. this incorrect coding style. */
  113. extern ULONGEST align_up (ULONGEST v, int n);
  114. extern ULONGEST align_down (ULONGEST v, int n);
  115. /* Convert hex digit A to a number, or throw an exception. */
  116. extern int fromhex (int a);
  117. /* HEX is a string of characters representing hexadecimal digits.
  118. Convert pairs of hex digits to bytes and store sequentially into
  119. BIN. COUNT is the maximum number of characters to convert. This
  120. will convert fewer characters if the number of hex characters
  121. actually seen is odd, or if HEX terminates before COUNT characters.
  122. Returns the number of characters actually converted. */
  123. extern int hex2bin (const char *hex, gdb_byte *bin, int count);
  124. /* Like the above, but return a gdb::byte_vector. */
  125. gdb::byte_vector hex2bin (const char *hex);
  126. #endif /* COMMON_COMMON_UTILS_H */