18dffb485Schristos /* Shared general utility routines for GDB, the GNU debugger. 28dffb485Schristos 3*5ba1f45fSchristos Copyright (C) 1986-2024 Free Software Foundation, Inc. 48dffb485Schristos 58dffb485Schristos This file is part of GDB. 68dffb485Schristos 78dffb485Schristos This program is free software; you can redistribute it and/or modify 88dffb485Schristos it under the terms of the GNU General Public License as published by 98dffb485Schristos the Free Software Foundation; either version 3 of the License, or 108dffb485Schristos (at your option) any later version. 118dffb485Schristos 128dffb485Schristos This program is distributed in the hope that it will be useful, 138dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 148dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158dffb485Schristos GNU General Public License for more details. 168dffb485Schristos 178dffb485Schristos You should have received a copy of the GNU General Public License 188dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #ifndef COMMON_COMMON_UTILS_H 218dffb485Schristos #define COMMON_COMMON_UTILS_H 228dffb485Schristos 238dffb485Schristos #include <string> 248dffb485Schristos #include <vector> 254b169a6bSchristos #include "gdbsupport/byte-vector.h" 264b169a6bSchristos #include "gdbsupport/gdb_unique_ptr.h" 27*5ba1f45fSchristos #include "gdbsupport/array-view.h" 288dffb485Schristos #include "poison.h" 29*5ba1f45fSchristos #include <string_view> 30*5ba1f45fSchristos 31*5ba1f45fSchristos #if defined HAVE_LIBXXHASH 32*5ba1f45fSchristos # include <xxhash.h> 33*5ba1f45fSchristos #else 34*5ba1f45fSchristos # include "hashtab.h" 35*5ba1f45fSchristos #endif 368dffb485Schristos 378dffb485Schristos /* xmalloc(), xrealloc() and xcalloc() have already been declared in 388dffb485Schristos "libiberty.h". */ 398dffb485Schristos 408dffb485Schristos /* Like xmalloc, but zero the memory. */ 418dffb485Schristos void *xzalloc (size_t); 428dffb485Schristos 438dffb485Schristos /* Like asprintf and vasprintf, but return the string, throw an error 448dffb485Schristos if no memory. */ 454b169a6bSchristos gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...) 464b169a6bSchristos ATTRIBUTE_PRINTF (1, 2); 474b169a6bSchristos gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap) 488dffb485Schristos ATTRIBUTE_PRINTF (1, 0); 498dffb485Schristos 508dffb485Schristos /* Like snprintf, but throw an error if the output buffer is too small. */ 518dffb485Schristos int xsnprintf (char *str, size_t size, const char *format, ...) 528dffb485Schristos ATTRIBUTE_PRINTF (3, 4); 538dffb485Schristos 548dffb485Schristos /* Returns a std::string built from a printf-style format string. */ 558dffb485Schristos std::string string_printf (const char* fmt, ...) 568dffb485Schristos ATTRIBUTE_PRINTF (1, 2); 578dffb485Schristos 588dffb485Schristos /* Like string_printf, but takes a va_list. */ 598dffb485Schristos std::string string_vprintf (const char* fmt, va_list args) 608dffb485Schristos ATTRIBUTE_PRINTF (1, 0); 618dffb485Schristos 628dffb485Schristos /* Like string_printf, but appends to DEST instead of returning a new 638dffb485Schristos std::string. */ 644b169a6bSchristos std::string &string_appendf (std::string &dest, const char* fmt, ...) 658dffb485Schristos ATTRIBUTE_PRINTF (2, 3); 668dffb485Schristos 678dffb485Schristos /* Like string_appendf, but takes a va_list. */ 684b169a6bSchristos std::string &string_vappendf (std::string &dest, const char* fmt, va_list args) 698dffb485Schristos ATTRIBUTE_PRINTF (2, 0); 708dffb485Schristos 718dffb485Schristos /* Make a copy of the string at PTR with LEN characters 728dffb485Schristos (and add a null character at the end in the copy). 738dffb485Schristos Uses malloc to get the space. Returns the address of the copy. */ 748dffb485Schristos 758dffb485Schristos char *savestring (const char *ptr, size_t len); 768dffb485Schristos 778dffb485Schristos /* Extract the next word from ARG. The next word is defined as either, 788dffb485Schristos everything up to the next space, or, if the next word starts with either 798dffb485Schristos a single or double quote, then everything up to the closing quote. The 808dffb485Schristos enclosing quotes are not returned in the result string. The pointer in 818dffb485Schristos ARG is updated to point to the first character after the end of the 828dffb485Schristos word, or, for quoted words, the first character after the closing 838dffb485Schristos quote. */ 848dffb485Schristos 858dffb485Schristos std::string extract_string_maybe_quoted (const char **arg); 868dffb485Schristos 878dffb485Schristos /* The strerror() function can return NULL for errno values that are 888dffb485Schristos out of range. Provide a "safe" version that always returns a 898dffb485Schristos printable string. This version is also thread-safe. */ 908dffb485Schristos 918dffb485Schristos extern const char *safe_strerror (int); 928dffb485Schristos 934b169a6bSchristos /* Version of startswith that takes string_view arguments. Return 944b169a6bSchristos true if the start of STRING matches PATTERN, false otherwise. */ 958dffb485Schristos 968dffb485Schristos static inline bool 97*5ba1f45fSchristos startswith (std::string_view string, std::string_view pattern) 988dffb485Schristos { 998dffb485Schristos return (string.length () >= pattern.length () 1008dffb485Schristos && strncmp (string.data (), pattern.data (), pattern.length ()) == 0); 1018dffb485Schristos } 1028dffb485Schristos 1034b169a6bSchristos /* Return true if the strings are equal. */ 1044b169a6bSchristos 1054b169a6bSchristos static inline bool 1064b169a6bSchristos streq (const char *lhs, const char *rhs) 1074b169a6bSchristos { 1084b169a6bSchristos return strcmp (lhs, rhs) == 0; 1094b169a6bSchristos } 1104b169a6bSchristos 1114b169a6bSchristos /* Compare C strings for std::sort. */ 1124b169a6bSchristos 1134b169a6bSchristos static inline bool 1144b169a6bSchristos compare_cstrings (const char *str1, const char *str2) 1154b169a6bSchristos { 1164b169a6bSchristos return strcmp (str1, str2) < 0; 1174b169a6bSchristos } 1184b169a6bSchristos 1198dffb485Schristos ULONGEST strtoulst (const char *num, const char **trailer, int base); 1208dffb485Schristos 1218dffb485Schristos /* Skip leading whitespace characters in INP, returning an updated 1228dffb485Schristos pointer. If INP is NULL, return NULL. */ 1238dffb485Schristos 1248dffb485Schristos extern char *skip_spaces (char *inp); 1258dffb485Schristos 1268dffb485Schristos /* A const-correct version of the above. */ 1278dffb485Schristos 1288dffb485Schristos extern const char *skip_spaces (const char *inp); 1298dffb485Schristos 1308dffb485Schristos /* Skip leading non-whitespace characters in INP, returning an updated 1318dffb485Schristos pointer. If INP is NULL, return NULL. */ 1328dffb485Schristos 1338dffb485Schristos extern char *skip_to_space (char *inp); 1348dffb485Schristos 1358dffb485Schristos /* A const-correct version of the above. */ 1368dffb485Schristos 1378dffb485Schristos extern const char *skip_to_space (const char *inp); 1388dffb485Schristos 1398dffb485Schristos /* Assumes that V is an argv for a program, and iterates through 1408dffb485Schristos freeing all the elements. */ 1418dffb485Schristos extern void free_vector_argv (std::vector<char *> &v); 1428dffb485Schristos 1438dffb485Schristos /* Return true if VALUE is in [LOW, HIGH]. */ 1448dffb485Schristos 1458dffb485Schristos template <typename T> 1468dffb485Schristos static bool 1478dffb485Schristos in_inclusive_range (T value, T low, T high) 1488dffb485Schristos { 1498dffb485Schristos return value >= low && value <= high; 1508dffb485Schristos } 1518dffb485Schristos 1524b169a6bSchristos /* Ensure that V is aligned to an N byte boundary (N's assumed to be a 1538dffb485Schristos power of 2). Round up/down when necessary. Examples of correct 1548dffb485Schristos use include: 1558dffb485Schristos 1568dffb485Schristos addr = align_up (addr, 8); -- VALUE needs 8 byte alignment 1578dffb485Schristos write_memory (addr, value, len); 1588dffb485Schristos addr += len; 1598dffb485Schristos 1608dffb485Schristos and: 1618dffb485Schristos 1628dffb485Schristos sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned 1638dffb485Schristos write_memory (sp, value, len); 1648dffb485Schristos 1658dffb485Schristos Note that uses such as: 1668dffb485Schristos 1678dffb485Schristos write_memory (addr, value, len); 1688dffb485Schristos addr += align_up (len, 8); 1698dffb485Schristos 1708dffb485Schristos and: 1718dffb485Schristos 1728dffb485Schristos sp -= align_up (len, 8); 1738dffb485Schristos write_memory (sp, value, len); 1748dffb485Schristos 1758dffb485Schristos are typically not correct as they don't ensure that the address (SP 1768dffb485Schristos or ADDR) is correctly aligned (relying on previous alignment to 1778dffb485Schristos keep things right). This is also why the methods are called 1788dffb485Schristos "align_..." instead of "round_..." as the latter reads better with 1798dffb485Schristos this incorrect coding style. */ 1808dffb485Schristos 1818dffb485Schristos extern ULONGEST align_up (ULONGEST v, int n); 1828dffb485Schristos extern ULONGEST align_down (ULONGEST v, int n); 1838dffb485Schristos 1844b169a6bSchristos /* Convert hex digit A to a number, or throw an exception. */ 1854b169a6bSchristos extern int fromhex (int a); 1864b169a6bSchristos 1874b169a6bSchristos /* HEX is a string of characters representing hexadecimal digits. 1884b169a6bSchristos Convert pairs of hex digits to bytes and store sequentially into 1894b169a6bSchristos BIN. COUNT is the maximum number of characters to convert. This 1904b169a6bSchristos will convert fewer characters if the number of hex characters 1914b169a6bSchristos actually seen is odd, or if HEX terminates before COUNT characters. 1924b169a6bSchristos Returns the number of characters actually converted. */ 1934b169a6bSchristos extern int hex2bin (const char *hex, gdb_byte *bin, int count); 1944b169a6bSchristos 1954b169a6bSchristos /* Like the above, but return a gdb::byte_vector. */ 1964b169a6bSchristos gdb::byte_vector hex2bin (const char *hex); 1974b169a6bSchristos 198*5ba1f45fSchristos /* Build a string containing the contents of BYTES. Each byte is 199*5ba1f45fSchristos represented as a 2 character hex string, with spaces separating each 200*5ba1f45fSchristos individual byte. */ 201*5ba1f45fSchristos 202*5ba1f45fSchristos extern std::string bytes_to_string (gdb::array_view<const gdb_byte> bytes); 203*5ba1f45fSchristos 204*5ba1f45fSchristos /* See bytes_to_string above. This takes a BUFFER pointer and LENGTH 205*5ba1f45fSchristos rather than an array view. */ 206*5ba1f45fSchristos 207*5ba1f45fSchristos static inline std::string bytes_to_string (const gdb_byte *buffer, 208*5ba1f45fSchristos size_t length) 209*5ba1f45fSchristos { 210*5ba1f45fSchristos return bytes_to_string ({buffer, length}); 211*5ba1f45fSchristos } 212*5ba1f45fSchristos 213*5ba1f45fSchristos /* A fast hashing function. This can be used to hash data in a fast way 214*5ba1f45fSchristos when the length is known. If no fast hashing library is available, falls 215*5ba1f45fSchristos back to iterative_hash from libiberty. START_VALUE can be set to 216*5ba1f45fSchristos continue hashing from a previous value. */ 217*5ba1f45fSchristos 218*5ba1f45fSchristos static inline unsigned int 219*5ba1f45fSchristos fast_hash (const void *ptr, size_t len, unsigned int start_value = 0) 220*5ba1f45fSchristos { 221*5ba1f45fSchristos #if defined HAVE_LIBXXHASH 222*5ba1f45fSchristos return XXH64 (ptr, len, start_value); 223*5ba1f45fSchristos #else 224*5ba1f45fSchristos return iterative_hash (ptr, len, start_value); 225*5ba1f45fSchristos #endif 226*5ba1f45fSchristos } 227*5ba1f45fSchristos 228*5ba1f45fSchristos namespace gdb 229*5ba1f45fSchristos { 230*5ba1f45fSchristos 231*5ba1f45fSchristos /* Hash type for std::string_view. 232*5ba1f45fSchristos 233*5ba1f45fSchristos Even after we switch to C++17 and dump our string_view implementation, we 234*5ba1f45fSchristos might want to keep this hash implementation if it's faster than std::hash 235*5ba1f45fSchristos for std::string_view. */ 236*5ba1f45fSchristos 237*5ba1f45fSchristos struct string_view_hash 238*5ba1f45fSchristos { 239*5ba1f45fSchristos std::size_t operator() (std::string_view view) const 240*5ba1f45fSchristos { return fast_hash (view.data (), view.length ()); } 241*5ba1f45fSchristos }; 242*5ba1f45fSchristos 243*5ba1f45fSchristos } /* namespace gdb */ 244*5ba1f45fSchristos 2458dffb485Schristos #endif /* COMMON_COMMON_UTILS_H */ 246