Commit | Line | Data |
---|---|---|
d26e3629 KY |
1 | /* Shared general utility routines for GDB, the GNU debugger. |
2 | ||
3666a048 | 3 | Copyright (C) 1986-2021 Free Software Foundation, Inc. |
d26e3629 KY |
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 | ||
1a5c2598 TT |
20 | #ifndef COMMON_COMMON_UTILS_H |
21 | #define COMMON_COMMON_UTILS_H | |
d26e3629 | 22 | |
d4081a38 | 23 | #include <string> |
7c5ded6a | 24 | #include <vector> |
d4081a38 | 25 | |
8172f16b SM |
26 | #include "poison.h" |
27 | ||
3a80edfc JB |
28 | /* If possible, define FUNCTION_NAME, a macro containing the name of |
29 | the function being defined. Since this macro may not always be | |
30 | defined, all uses must be protected by appropriate macro definition | |
31 | checks (Eg: "#ifdef FUNCTION_NAME"). | |
32 | ||
33 | Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__' | |
df049a58 DE |
34 | which contains the name of the function currently being defined. |
35 | This is broken in G++ before version 2.6. | |
36 | C9x has a similar variable called __func__, but prefer the GCC one since | |
37 | it demangles C++ function names. */ | |
38 | #if (GCC_VERSION >= 2004) | |
39 | #define FUNCTION_NAME __PRETTY_FUNCTION__ | |
40 | #else | |
41 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L | |
46bbb3ed | 42 | #define FUNCTION_NAME __func__ /* ARI: func */ |
df049a58 DE |
43 | #endif |
44 | #endif | |
45 | ||
87f34879 CB |
46 | #include "gdb_string_view.h" |
47 | ||
d26e3629 KY |
48 | /* xmalloc(), xrealloc() and xcalloc() have already been declared in |
49 | "libiberty.h". */ | |
50 | ||
51 | /* Like xmalloc, but zero the memory. */ | |
52 | void *xzalloc (size_t); | |
53 | ||
8172f16b SM |
54 | template <typename T> |
55 | static void | |
56 | xfree (T *ptr) | |
57 | { | |
58 | static_assert (IsFreeable<T>::value, "Trying to use xfree with a non-POD \ | |
59 | data type. Use operator delete instead."); | |
60 | ||
61 | if (ptr != NULL) | |
9c9d63b1 PM |
62 | #ifdef GNULIB_NAMESPACE |
63 | GNULIB_NAMESPACE::free (ptr); /* ARI: free */ | |
64 | #else | |
65 | free (ptr); /* ARI: free */ | |
66 | #endif | |
8172f16b SM |
67 | } |
68 | ||
d26e3629 KY |
69 | |
70 | /* Like asprintf and vasprintf, but return the string, throw an error | |
71 | if no memory. */ | |
72 | char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2); | |
73 | char *xstrvprintf (const char *format, va_list ap) | |
74 | ATTRIBUTE_PRINTF (1, 0); | |
75 | ||
d26e3629 KY |
76 | /* Like snprintf, but throw an error if the output buffer is too small. */ |
77 | int xsnprintf (char *str, size_t size, const char *format, ...) | |
78 | ATTRIBUTE_PRINTF (3, 4); | |
79 | ||
d4081a38 PA |
80 | /* Returns a std::string built from a printf-style format string. */ |
81 | std::string string_printf (const char* fmt, ...) | |
82 | ATTRIBUTE_PRINTF (1, 2); | |
83 | ||
bd413795 TT |
84 | /* Like string_printf, but takes a va_list. */ |
85 | std::string string_vprintf (const char* fmt, va_list args) | |
86 | ATTRIBUTE_PRINTF (1, 0); | |
87 | ||
31b833b3 PA |
88 | /* Like string_printf, but appends to DEST instead of returning a new |
89 | std::string. */ | |
90 | void string_appendf (std::string &dest, const char* fmt, ...) | |
91 | ATTRIBUTE_PRINTF (2, 3); | |
92 | ||
93 | /* Like string_appendf, but takes a va_list. */ | |
94 | void string_vappendf (std::string &dest, const char* fmt, va_list args) | |
95 | ATTRIBUTE_PRINTF (2, 0); | |
96 | ||
baea0dae PA |
97 | /* Make a copy of the string at PTR with LEN characters |
98 | (and add a null character at the end in the copy). | |
99 | Uses malloc to get the space. Returns the address of the copy. */ | |
100 | ||
101 | char *savestring (const char *ptr, size_t len); | |
102 | ||
021d8588 AB |
103 | /* Extract the next word from ARG. The next word is defined as either, |
104 | everything up to the next space, or, if the next word starts with either | |
105 | a single or double quote, then everything up to the closing quote. The | |
106 | enclosing quotes are not returned in the result string. The pointer in | |
107 | ARG is updated to point to the first character after the end of the | |
108 | word, or, for quoted words, the first character after the closing | |
109 | quote. */ | |
110 | ||
111 | std::string extract_string_maybe_quoted (const char **arg); | |
112 | ||
fb23d554 SDJ |
113 | /* The strerror() function can return NULL for errno values that are |
114 | out of range. Provide a "safe" version that always returns a | |
b231e86a | 115 | printable string. This version is also thread-safe. */ |
fb23d554 | 116 | |
b231e86a | 117 | extern const char *safe_strerror (int); |
fb23d554 | 118 | |
23771117 | 119 | /* Return true if the start of STRING matches PATTERN, false otherwise. */ |
61012eef | 120 | |
23771117 | 121 | static inline bool |
61012eef GB |
122 | startswith (const char *string, const char *pattern) |
123 | { | |
124 | return strncmp (string, pattern, strlen (pattern)) == 0; | |
125 | } | |
126 | ||
87f34879 CB |
127 | /* Version of startswith that takes string_view arguments. See comment |
128 | above. */ | |
129 | ||
130 | static inline bool | |
131 | startswith (gdb::string_view string, gdb::string_view pattern) | |
132 | { | |
133 | return (string.length () >= pattern.length () | |
134 | && strncmp (string.data (), pattern.data (), pattern.length ()) == 0); | |
135 | } | |
136 | ||
03aef70f JK |
137 | ULONGEST strtoulst (const char *num, const char **trailer, int base); |
138 | ||
139 | /* Skip leading whitespace characters in INP, returning an updated | |
140 | pointer. If INP is NULL, return NULL. */ | |
141 | ||
142 | extern char *skip_spaces (char *inp); | |
143 | ||
144 | /* A const-correct version of the above. */ | |
145 | ||
f1735a53 | 146 | extern const char *skip_spaces (const char *inp); |
03aef70f JK |
147 | |
148 | /* Skip leading non-whitespace characters in INP, returning an updated | |
149 | pointer. If INP is NULL, return NULL. */ | |
150 | ||
f1735a53 | 151 | extern char *skip_to_space (char *inp); |
03aef70f JK |
152 | |
153 | /* A const-correct version of the above. */ | |
154 | ||
f1735a53 | 155 | extern const char *skip_to_space (const char *inp); |
03aef70f | 156 | |
7c5ded6a SDJ |
157 | /* Assumes that V is an argv for a program, and iterates through |
158 | freeing all the elements. */ | |
159 | extern void free_vector_argv (std::vector<char *> &v); | |
160 | ||
b020ff80 SM |
161 | /* Return true if VALUE is in [LOW, HIGH]. */ |
162 | ||
163 | template <typename T> | |
164 | static bool | |
165 | in_inclusive_range (T value, T low, T high) | |
166 | { | |
167 | return value >= low && value <= high; | |
168 | } | |
169 | ||
4b186f88 | 170 | /* Ensure that V is aligned to an N byte boundary (N's assumed to be a |
a3b60e45 JK |
171 | power of 2). Round up/down when necessary. Examples of correct |
172 | use include: | |
173 | ||
174 | addr = align_up (addr, 8); -- VALUE needs 8 byte alignment | |
175 | write_memory (addr, value, len); | |
176 | addr += len; | |
177 | ||
178 | and: | |
179 | ||
180 | sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned | |
181 | write_memory (sp, value, len); | |
182 | ||
183 | Note that uses such as: | |
184 | ||
185 | write_memory (addr, value, len); | |
186 | addr += align_up (len, 8); | |
187 | ||
188 | and: | |
189 | ||
190 | sp -= align_up (len, 8); | |
191 | write_memory (sp, value, len); | |
192 | ||
193 | are typically not correct as they don't ensure that the address (SP | |
194 | or ADDR) is correctly aligned (relying on previous alignment to | |
195 | keep things right). This is also why the methods are called | |
196 | "align_..." instead of "round_..." as the latter reads better with | |
197 | this incorrect coding style. */ | |
198 | ||
199 | extern ULONGEST align_up (ULONGEST v, int n); | |
200 | extern ULONGEST align_down (ULONGEST v, int n); | |
201 | ||
1a5c2598 | 202 | #endif /* COMMON_COMMON_UTILS_H */ |