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