gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdbsupport / common-utils.h
CommitLineData
d26e3629
KY
1/* Shared general utility routines for GDB, the GNU debugger.
2
b811d2c2 3 Copyright (C) 1986-2020 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. */
52void *xzalloc (size_t);
53
8172f16b
SM
54template <typename T>
55static void
56xfree (T *ptr)
57{
58 static_assert (IsFreeable<T>::value, "Trying to use xfree with a non-POD \
59data type. Use operator delete instead.");
60
61 if (ptr != NULL)
62 free (ptr); /* ARI: free */
63}
64
d26e3629
KY
65
66/* Like asprintf and vasprintf, but return the string, throw an error
67 if no memory. */
68char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
69char *xstrvprintf (const char *format, va_list ap)
70 ATTRIBUTE_PRINTF (1, 0);
71
d26e3629
KY
72/* Like snprintf, but throw an error if the output buffer is too small. */
73int xsnprintf (char *str, size_t size, const char *format, ...)
74 ATTRIBUTE_PRINTF (3, 4);
75
d4081a38
PA
76/* Returns a std::string built from a printf-style format string. */
77std::string string_printf (const char* fmt, ...)
78 ATTRIBUTE_PRINTF (1, 2);
79
bd413795
TT
80/* Like string_printf, but takes a va_list. */
81std::string string_vprintf (const char* fmt, va_list args)
82 ATTRIBUTE_PRINTF (1, 0);
83
31b833b3
PA
84/* Like string_printf, but appends to DEST instead of returning a new
85 std::string. */
86void string_appendf (std::string &dest, const char* fmt, ...)
87 ATTRIBUTE_PRINTF (2, 3);
88
89/* Like string_appendf, but takes a va_list. */
90void string_vappendf (std::string &dest, const char* fmt, va_list args)
91 ATTRIBUTE_PRINTF (2, 0);
92
baea0dae
PA
93/* Make a copy of the string at PTR with LEN characters
94 (and add a null character at the end in the copy).
95 Uses malloc to get the space. Returns the address of the copy. */
96
97char *savestring (const char *ptr, size_t len);
98
021d8588
AB
99/* Extract the next word from ARG. The next word is defined as either,
100 everything up to the next space, or, if the next word starts with either
101 a single or double quote, then everything up to the closing quote. The
102 enclosing quotes are not returned in the result string. The pointer in
103 ARG is updated to point to the first character after the end of the
104 word, or, for quoted words, the first character after the closing
105 quote. */
106
107std::string extract_string_maybe_quoted (const char **arg);
108
fb23d554
SDJ
109/* The strerror() function can return NULL for errno values that are
110 out of range. Provide a "safe" version that always returns a
b231e86a 111 printable string. This version is also thread-safe. */
fb23d554 112
b231e86a 113extern const char *safe_strerror (int);
fb23d554 114
23771117 115/* Return true if the start of STRING matches PATTERN, false otherwise. */
61012eef 116
23771117 117static inline bool
61012eef
GB
118startswith (const char *string, const char *pattern)
119{
120 return strncmp (string, pattern, strlen (pattern)) == 0;
121}
122
87f34879
CB
123/* Version of startswith that takes string_view arguments. See comment
124 above. */
125
126static inline bool
127startswith (gdb::string_view string, gdb::string_view pattern)
128{
129 return (string.length () >= pattern.length ()
130 && strncmp (string.data (), pattern.data (), pattern.length ()) == 0);
131}
132
03aef70f
JK
133ULONGEST strtoulst (const char *num, const char **trailer, int base);
134
135/* Skip leading whitespace characters in INP, returning an updated
136 pointer. If INP is NULL, return NULL. */
137
138extern char *skip_spaces (char *inp);
139
140/* A const-correct version of the above. */
141
f1735a53 142extern const char *skip_spaces (const char *inp);
03aef70f
JK
143
144/* Skip leading non-whitespace characters in INP, returning an updated
145 pointer. If INP is NULL, return NULL. */
146
f1735a53 147extern char *skip_to_space (char *inp);
03aef70f
JK
148
149/* A const-correct version of the above. */
150
f1735a53 151extern const char *skip_to_space (const char *inp);
03aef70f 152
7c5ded6a
SDJ
153/* Assumes that V is an argv for a program, and iterates through
154 freeing all the elements. */
155extern void free_vector_argv (std::vector<char *> &v);
156
b020ff80
SM
157/* Return true if VALUE is in [LOW, HIGH]. */
158
159template <typename T>
160static bool
161in_inclusive_range (T value, T low, T high)
162{
163 return value >= low && value <= high;
164}
165
a3b60e45
JK
166/* Ensure that V is aligned to an N byte boundary (B's assumed to be a
167 power of 2). Round up/down when necessary. Examples of correct
168 use include:
169
170 addr = align_up (addr, 8); -- VALUE needs 8 byte alignment
171 write_memory (addr, value, len);
172 addr += len;
173
174 and:
175
176 sp = align_down (sp - len, 16); -- Keep SP 16 byte aligned
177 write_memory (sp, value, len);
178
179 Note that uses such as:
180
181 write_memory (addr, value, len);
182 addr += align_up (len, 8);
183
184 and:
185
186 sp -= align_up (len, 8);
187 write_memory (sp, value, len);
188
189 are typically not correct as they don't ensure that the address (SP
190 or ADDR) is correctly aligned (relying on previous alignment to
191 keep things right). This is also why the methods are called
192 "align_..." instead of "round_..." as the latter reads better with
193 this incorrect coding style. */
194
195extern ULONGEST align_up (ULONGEST v, int n);
196extern ULONGEST align_down (ULONGEST v, int n);
197
1a5c2598 198#endif /* COMMON_COMMON_UTILS_H */
This page took 0.609346 seconds and 4 git commands to generate.