xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/common-utils.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Shared general utility routines for GDB, the GNU debugger.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 1986-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #ifndef COMMON_COMMON_UTILS_H
217d62b00eSchristos #define COMMON_COMMON_UTILS_H
227d62b00eSchristos 
237d62b00eSchristos #include <string>
247d62b00eSchristos #include <vector>
25*6881a400Schristos #include "gdbsupport/byte-vector.h"
26*6881a400Schristos #include "gdbsupport/gdb_unique_ptr.h"
277d62b00eSchristos #include "poison.h"
287d62b00eSchristos #include "gdb_string_view.h"
297d62b00eSchristos 
307d62b00eSchristos /* xmalloc(), xrealloc() and xcalloc() have already been declared in
317d62b00eSchristos    "libiberty.h". */
327d62b00eSchristos 
337d62b00eSchristos /* Like xmalloc, but zero the memory.  */
347d62b00eSchristos void *xzalloc (size_t);
357d62b00eSchristos 
367d62b00eSchristos /* Like asprintf and vasprintf, but return the string, throw an error
377d62b00eSchristos    if no memory.  */
38*6881a400Schristos gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...)
39*6881a400Schristos      ATTRIBUTE_PRINTF (1, 2);
40*6881a400Schristos gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap)
417d62b00eSchristos      ATTRIBUTE_PRINTF (1, 0);
427d62b00eSchristos 
437d62b00eSchristos /* Like snprintf, but throw an error if the output buffer is too small.  */
447d62b00eSchristos int xsnprintf (char *str, size_t size, const char *format, ...)
457d62b00eSchristos      ATTRIBUTE_PRINTF (3, 4);
467d62b00eSchristos 
477d62b00eSchristos /* Returns a std::string built from a printf-style format string.  */
487d62b00eSchristos std::string string_printf (const char* fmt, ...)
497d62b00eSchristos   ATTRIBUTE_PRINTF (1, 2);
507d62b00eSchristos 
517d62b00eSchristos /* Like string_printf, but takes a va_list.  */
527d62b00eSchristos std::string string_vprintf (const char* fmt, va_list args)
537d62b00eSchristos   ATTRIBUTE_PRINTF (1, 0);
547d62b00eSchristos 
557d62b00eSchristos /* Like string_printf, but appends to DEST instead of returning a new
567d62b00eSchristos    std::string.  */
57*6881a400Schristos std::string &string_appendf (std::string &dest, const char* fmt, ...)
587d62b00eSchristos   ATTRIBUTE_PRINTF (2, 3);
597d62b00eSchristos 
607d62b00eSchristos /* Like string_appendf, but takes a va_list.  */
61*6881a400Schristos std::string &string_vappendf (std::string &dest, const char* fmt, va_list args)
627d62b00eSchristos   ATTRIBUTE_PRINTF (2, 0);
637d62b00eSchristos 
647d62b00eSchristos /* Make a copy of the string at PTR with LEN characters
657d62b00eSchristos    (and add a null character at the end in the copy).
667d62b00eSchristos    Uses malloc to get the space.  Returns the address of the copy.  */
677d62b00eSchristos 
687d62b00eSchristos char *savestring (const char *ptr, size_t len);
697d62b00eSchristos 
707d62b00eSchristos /* Extract the next word from ARG.  The next word is defined as either,
717d62b00eSchristos    everything up to the next space, or, if the next word starts with either
727d62b00eSchristos    a single or double quote, then everything up to the closing quote.  The
737d62b00eSchristos    enclosing quotes are not returned in the result string.  The pointer in
747d62b00eSchristos    ARG is updated to point to the first character after the end of the
757d62b00eSchristos    word, or, for quoted words, the first character after the closing
767d62b00eSchristos    quote.  */
777d62b00eSchristos 
787d62b00eSchristos std::string extract_string_maybe_quoted (const char **arg);
797d62b00eSchristos 
807d62b00eSchristos /* The strerror() function can return NULL for errno values that are
817d62b00eSchristos    out of range.  Provide a "safe" version that always returns a
827d62b00eSchristos    printable string.  This version is also thread-safe.  */
837d62b00eSchristos 
847d62b00eSchristos extern const char *safe_strerror (int);
857d62b00eSchristos 
86*6881a400Schristos /* Version of startswith that takes string_view arguments.  Return
87*6881a400Schristos    true if the start of STRING matches PATTERN, false otherwise.  */
887d62b00eSchristos 
897d62b00eSchristos static inline bool
907d62b00eSchristos startswith (gdb::string_view string, gdb::string_view pattern)
917d62b00eSchristos {
927d62b00eSchristos   return (string.length () >= pattern.length ()
937d62b00eSchristos 	  && strncmp (string.data (), pattern.data (), pattern.length ()) == 0);
947d62b00eSchristos }
957d62b00eSchristos 
96*6881a400Schristos /* Return true if the strings are equal.  */
97*6881a400Schristos 
98*6881a400Schristos static inline bool
99*6881a400Schristos streq (const char *lhs, const char *rhs)
100*6881a400Schristos {
101*6881a400Schristos   return strcmp (lhs, rhs) == 0;
102*6881a400Schristos }
103*6881a400Schristos 
104*6881a400Schristos /* Compare C strings for std::sort.  */
105*6881a400Schristos 
106*6881a400Schristos static inline bool
107*6881a400Schristos compare_cstrings (const char *str1, const char *str2)
108*6881a400Schristos {
109*6881a400Schristos   return strcmp (str1, str2) < 0;
110*6881a400Schristos }
111*6881a400Schristos 
1127d62b00eSchristos ULONGEST strtoulst (const char *num, const char **trailer, int base);
1137d62b00eSchristos 
1147d62b00eSchristos /* Skip leading whitespace characters in INP, returning an updated
1157d62b00eSchristos    pointer.  If INP is NULL, return NULL.  */
1167d62b00eSchristos 
1177d62b00eSchristos extern char *skip_spaces (char *inp);
1187d62b00eSchristos 
1197d62b00eSchristos /* A const-correct version of the above.  */
1207d62b00eSchristos 
1217d62b00eSchristos extern const char *skip_spaces (const char *inp);
1227d62b00eSchristos 
1237d62b00eSchristos /* Skip leading non-whitespace characters in INP, returning an updated
1247d62b00eSchristos    pointer.  If INP is NULL, return NULL.  */
1257d62b00eSchristos 
1267d62b00eSchristos extern char *skip_to_space (char *inp);
1277d62b00eSchristos 
1287d62b00eSchristos /* A const-correct version of the above.  */
1297d62b00eSchristos 
1307d62b00eSchristos extern const char *skip_to_space (const char *inp);
1317d62b00eSchristos 
1327d62b00eSchristos /* Assumes that V is an argv for a program, and iterates through
1337d62b00eSchristos    freeing all the elements.  */
1347d62b00eSchristos extern void free_vector_argv (std::vector<char *> &v);
1357d62b00eSchristos 
1367d62b00eSchristos /* Return true if VALUE is in [LOW, HIGH].  */
1377d62b00eSchristos 
1387d62b00eSchristos template <typename T>
1397d62b00eSchristos static bool
1407d62b00eSchristos in_inclusive_range (T value, T low, T high)
1417d62b00eSchristos {
1427d62b00eSchristos   return value >= low && value <= high;
1437d62b00eSchristos }
1447d62b00eSchristos 
145*6881a400Schristos /* Ensure that V is aligned to an N byte boundary (N's assumed to be a
1467d62b00eSchristos    power of 2).  Round up/down when necessary.  Examples of correct
1477d62b00eSchristos    use include:
1487d62b00eSchristos 
1497d62b00eSchristos     addr = align_up (addr, 8); -- VALUE needs 8 byte alignment
1507d62b00eSchristos     write_memory (addr, value, len);
1517d62b00eSchristos     addr += len;
1527d62b00eSchristos 
1537d62b00eSchristos    and:
1547d62b00eSchristos 
1557d62b00eSchristos     sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned
1567d62b00eSchristos     write_memory (sp, value, len);
1577d62b00eSchristos 
1587d62b00eSchristos    Note that uses such as:
1597d62b00eSchristos 
1607d62b00eSchristos     write_memory (addr, value, len);
1617d62b00eSchristos     addr += align_up (len, 8);
1627d62b00eSchristos 
1637d62b00eSchristos    and:
1647d62b00eSchristos 
1657d62b00eSchristos     sp -= align_up (len, 8);
1667d62b00eSchristos     write_memory (sp, value, len);
1677d62b00eSchristos 
1687d62b00eSchristos    are typically not correct as they don't ensure that the address (SP
1697d62b00eSchristos    or ADDR) is correctly aligned (relying on previous alignment to
1707d62b00eSchristos    keep things right).  This is also why the methods are called
1717d62b00eSchristos    "align_..." instead of "round_..." as the latter reads better with
1727d62b00eSchristos    this incorrect coding style.  */
1737d62b00eSchristos 
1747d62b00eSchristos extern ULONGEST align_up (ULONGEST v, int n);
1757d62b00eSchristos extern ULONGEST align_down (ULONGEST v, int n);
1767d62b00eSchristos 
177*6881a400Schristos /* Convert hex digit A to a number, or throw an exception.  */
178*6881a400Schristos extern int fromhex (int a);
179*6881a400Schristos 
180*6881a400Schristos /* HEX is a string of characters representing hexadecimal digits.
181*6881a400Schristos    Convert pairs of hex digits to bytes and store sequentially into
182*6881a400Schristos    BIN.  COUNT is the maximum number of characters to convert.  This
183*6881a400Schristos    will convert fewer characters if the number of hex characters
184*6881a400Schristos    actually seen is odd, or if HEX terminates before COUNT characters.
185*6881a400Schristos    Returns the number of characters actually converted.  */
186*6881a400Schristos extern int hex2bin (const char *hex, gdb_byte *bin, int count);
187*6881a400Schristos 
188*6881a400Schristos /* Like the above, but return a gdb::byte_vector.  */
189*6881a400Schristos gdb::byte_vector hex2bin (const char *hex);
190*6881a400Schristos 
1917d62b00eSchristos #endif /* COMMON_COMMON_UTILS_H */
192