1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef COMMON_COMMON_UTILS_H
21 #define COMMON_COMMON_UTILS_H
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").
33 Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
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__
41 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
42 #define FUNCTION_NAME __func__ /* ARI: func */
46 /* xmalloc(), xrealloc() and xcalloc() have already been declared in
49 /* Like xmalloc, but zero the memory. */
50 void *xzalloc (size_t);
56 static_assert (IsFreeable<T>::value, "Trying to use xfree with a non-POD \
57 data type. Use operator delete instead.");
60 free (ptr); /* ARI: free */
64 /* Like asprintf and vasprintf, but return the string, throw an error
66 char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
67 char *xstrvprintf (const char *format, va_list ap)
68 ATTRIBUTE_PRINTF (1, 0);
70 /* Like snprintf, but throw an error if the output buffer is too small. */
71 int xsnprintf (char *str, size_t size, const char *format, ...)
72 ATTRIBUTE_PRINTF (3, 4);
74 /* Returns a std::string built from a printf-style format string. */
75 std::string string_printf (const char* fmt, ...)
76 ATTRIBUTE_PRINTF (1, 2);
78 /* Like string_printf, but takes a va_list. */
79 std::string string_vprintf (const char* fmt, va_list args)
80 ATTRIBUTE_PRINTF (1, 0);
82 /* Like string_printf, but appends to DEST instead of returning a new
84 void string_appendf (std::string &dest, const char* fmt, ...)
85 ATTRIBUTE_PRINTF (2, 3);
87 /* Like string_appendf, but takes a va_list. */
88 void string_vappendf (std::string &dest, const char* fmt, va_list args)
89 ATTRIBUTE_PRINTF (2, 0);
91 /* Make a copy of the string at PTR with LEN characters
92 (and add a null character at the end in the copy).
93 Uses malloc to get the space. Returns the address of the copy. */
95 char *savestring (const char *ptr, size_t len);
97 /* Extract the next word from ARG. The next word is defined as either,
98 everything up to the next space, or, if the next word starts with either
99 a single or double quote, then everything up to the closing quote. The
100 enclosing quotes are not returned in the result string. The pointer in
101 ARG is updated to point to the first character after the end of the
102 word, or, for quoted words, the first character after the closing
105 std::string extract_string_maybe_quoted (const char **arg);
107 /* The strerror() function can return NULL for errno values that are
108 out of range. Provide a "safe" version that always returns a
111 extern char *safe_strerror (int);
113 /* Return non-zero if the start of STRING matches PATTERN, zero
117 startswith (const char *string, const char *pattern)
119 return strncmp (string, pattern, strlen (pattern)) == 0;
122 ULONGEST strtoulst (const char *num, const char **trailer, int base);
124 /* Skip leading whitespace characters in INP, returning an updated
125 pointer. If INP is NULL, return NULL. */
127 extern char *skip_spaces (char *inp);
129 /* A const-correct version of the above. */
131 extern const char *skip_spaces (const char *inp);
133 /* Skip leading non-whitespace characters in INP, returning an updated
134 pointer. If INP is NULL, return NULL. */
136 extern char *skip_to_space (char *inp);
138 /* A const-correct version of the above. */
140 extern const char *skip_to_space (const char *inp);
142 /* Assumes that V is an argv for a program, and iterates through
143 freeing all the elements. */
144 extern void free_vector_argv (std::vector<char *> &v);
146 /* Given a vector of arguments ARGV, return a string equivalent to
147 joining all the arguments with a whitespace separating them. */
148 extern std::string stringify_argv (const std::vector<char *> &argv);
150 /* Return true if VALUE is in [LOW, HIGH]. */
152 template <typename T>
154 in_inclusive_range (T value, T low, T high)
156 return value >= low && value <= high;
159 /* Ensure that V is aligned to an N byte boundary (B's assumed to be a
160 power of 2). Round up/down when necessary. Examples of correct
163 addr = align_up (addr, 8); -- VALUE needs 8 byte alignment
164 write_memory (addr, value, len);
169 sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned
170 write_memory (sp, value, len);
172 Note that uses such as:
174 write_memory (addr, value, len);
175 addr += align_up (len, 8);
179 sp -= align_up (len, 8);
180 write_memory (sp, value, len);
182 are typically not correct as they don't ensure that the address (SP
183 or ADDR) is correctly aligned (relying on previous alignment to
184 keep things right). This is also why the methods are called
185 "align_..." instead of "round_..." as the latter reads better with
186 this incorrect coding style. */
188 extern ULONGEST align_up (ULONGEST v, int n);
189 extern ULONGEST align_down (ULONGEST v, int n);
191 #endif /* COMMON_COMMON_UTILS_H */