| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
#include "cleanup.h" |
| 7 |
#include "debug_info_options.h" |
| 8 |
#include "string_builder.h" |
| 9 |
#include "util.h" |
| 10 |
|
| 11 |
static const char * const drgn_debug_info_options_default_directories[] = { |
| 12 |
"/usr/lib/debug", NULL |
| 13 |
}; |
| 14 |
static const bool drgn_debug_info_options_directories_allow_empty = false; |
| 15 |
|
| 16 |
static const char * const drgn_debug_info_options_default_debug_link_directories[] = { |
| 17 |
"$ORIGIN", "$ORIGIN/.debug", "", NULL |
| 18 |
}; |
| 19 |
static const bool drgn_debug_info_options_debug_link_directories_allow_empty = true; |
| 20 |
|
| 21 |
static const char * const drgn_debug_info_options_default_kernel_directories[] = { |
| 22 |
"", NULL |
| 23 |
}; |
| 24 |
static const bool drgn_debug_info_options_kernel_directories_allow_empty = true; |
| 25 |
|
| 26 |
void drgn_debug_info_options_init(struct drgn_debug_info_options *options) |
| 27 |
{ |
| 28 |
#define LIST_OPTION(name) \ |
| 29 |
options->name = drgn_debug_info_options_default_##name; |
| 30 |
#define BOOL_OPTION(name, default_value) options->name = default_value; |
| 31 |
#define ENUM_OPTION(name, type, default_value) options->name = default_value; |
| 32 |
DRGN_DEBUG_INFO_OPTIONS |
| 33 |
#undef ENUM_OPTION |
| 34 |
#undef BOOL_OPTION |
| 35 |
#undef LIST_OPTION |
| 36 |
} |
| 37 |
|
| 38 |
static void drgn_debug_info_options_list_destroy(const char * const *list, |
| 39 |
const char * const *default_list) |
| 40 |
{ |
| 41 |
if (list && list != default_list) { |
| 42 |
for (size_t i = 0; list[i]; i++) |
| 43 |
free((void *)list[i]); |
| 44 |
free((void *)list); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
static void drgn_debug_info_options_listp_destroy(const char * const **listp) |
| 49 |
{ |
| 50 |
drgn_debug_info_options_list_destroy((const char * const *)*listp, |
| 51 |
NULL); |
| 52 |
} |
| 53 |
|
| 54 |
void drgn_debug_info_options_deinit(struct drgn_debug_info_options *options) |
| 55 |
{ |
| 56 |
#define LIST_OPTION(name) \ |
| 57 |
drgn_debug_info_options_list_destroy(options->name, \ |
| 58 |
drgn_debug_info_options_default_##name); |
| 59 |
#define BOOL_OPTION(name, default_value) |
| 60 |
#define ENUM_OPTION(name, type, default_value) |
| 61 |
DRGN_DEBUG_INFO_OPTIONS |
| 62 |
#undef ENUM_OPTION |
| 63 |
#undef BOOL_OPTION |
| 64 |
#undef LIST_OPTION |
| 65 |
} |
| 66 |
|
| 67 |
LIBDRGN_PUBLIC struct drgn_error * |
| 68 |
drgn_debug_info_options_create(struct drgn_debug_info_options **ret) |
| 69 |
{ |
| 70 |
struct drgn_debug_info_options *options = malloc(sizeof(*options)); |
| 71 |
if (!options) |
| 72 |
return &drgn_enomem; |
| 73 |
drgn_debug_info_options_init(options); |
| 74 |
*ret = options; |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
LIBDRGN_PUBLIC void |
| 79 |
drgn_debug_info_options_destroy(struct drgn_debug_info_options *options) |
| 80 |
{ |
| 81 |
if (options) { |
| 82 |
drgn_debug_info_options_deinit(options); |
| 83 |
free(options); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static struct drgn_error * |
| 88 |
drgn_debug_info_options_list_dup(const char * const *list, bool allow_empty, |
| 89 |
const char * const **ret) |
| 90 |
{ |
| 91 |
size_t n = 0; |
| 92 |
while (list[n]) { |
| 93 |
if (!allow_empty && !list[n][0]) { |
| 94 |
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT, |
| 95 |
"string cannot be empty"); |
| 96 |
} |
| 97 |
n++; |
| 98 |
} |
| 99 |
char **copy = malloc_array(n + 1, sizeof(copy[0])); |
| 100 |
if (!copy) |
| 101 |
return &drgn_enomem; |
| 102 |
for (size_t i = 0; i < n; i++) { |
| 103 |
copy[i] = strdup(list[i]); |
| 104 |
if (!copy[i]) { |
| 105 |
for (size_t j = 0; j < i; j++) |
| 106 |
free(copy[j]); |
| 107 |
free(copy); |
| 108 |
return &drgn_enomem; |
| 109 |
} |
| 110 |
} |
| 111 |
copy[n] = NULL; |
| 112 |
*ret = (const char * const *)copy; |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
LIBDRGN_PUBLIC struct drgn_error * |
| 117 |
drgn_debug_info_options_copy(struct drgn_debug_info_options *dst, |
| 118 |
const struct drgn_debug_info_options *src) |
| 119 |
{ |
| 120 |
struct drgn_error *err; |
| 121 |
if (dst == src) |
| 122 |
return NULL; |
| 123 |
|
| 124 |
// Since copying any list could fail, make all of the copies first. |
| 125 |
// Replace the default lists with NULL for now to avoid unnecessary |
| 126 |
// copies and simplify cleanup. |
| 127 |
#define LIST_OPTION(name) \ |
| 128 |
_cleanup_(drgn_debug_info_options_listp_destroy) \ |
| 129 |
const char * const *name##_copy = NULL; \ |
| 130 |
if (src->name != drgn_debug_info_options_default_##name) { \ |
| 131 |
err = drgn_debug_info_options_list_dup(src->name, true, \ |
| 132 |
&name##_copy); \ |
| 133 |
if (err) \ |
| 134 |
return err; \ |
| 135 |
} |
| 136 |
#define BOOL_OPTION(name, default_value) |
| 137 |
#define ENUM_OPTION(name, type, default_value) |
| 138 |
DRGN_DEBUG_INFO_OPTIONS |
| 139 |
#undef ENUM_OPTION |
| 140 |
#undef BOOL_OPTION |
| 141 |
#undef LIST_OPTION |
| 142 |
|
| 143 |
// Now we can set everything. |
| 144 |
#define LIST_OPTION(name) \ |
| 145 |
drgn_debug_info_options_list_destroy(dst->name, \ |
| 146 |
drgn_debug_info_options_default_##name);\ |
| 147 |
if (name##_copy) \ |
| 148 |
dst->name = no_cleanup_ptr(name##_copy); \ |
| 149 |
else \ |
| 150 |
dst->name = drgn_debug_info_options_default_##name; |
| 151 |
#define BOOL_OPTION(name, default_value) dst->name = src->name; |
| 152 |
#define ENUM_OPTION(name, type, default_value) dst->name = src->name; |
| 153 |
DRGN_DEBUG_INFO_OPTIONS |
| 154 |
#undef ENUM_OPTION |
| 155 |
#undef BOOL_OPTION |
| 156 |
#undef LIST_OPTION |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
#define DRGN_DEBUG_INFO_OPTIONS_GET(type, name) \ |
| 161 |
LIBDRGN_PUBLIC type \ |
| 162 |
drgn_debug_info_options_get_##name(const struct drgn_debug_info_options *options)\ |
| 163 |
{ \ |
| 164 |
return options->name; \ |
| 165 |
} |
| 166 |
|
| 167 |
#define DRGN_DEBUG_INFO_OPTIONS_GETSET(type, name) \ |
| 168 |
DRGN_DEBUG_INFO_OPTIONS_GET(type, name) \ |
| 169 |
\ |
| 170 |
LIBDRGN_PUBLIC void \ |
| 171 |
drgn_debug_info_options_set_##name(struct drgn_debug_info_options *options, \ |
| 172 |
type value) \ |
| 173 |
{ \ |
| 174 |
options->name = value; \ |
| 175 |
} |
| 176 |
|
| 177 |
#define LIST_OPTION(name) \ |
| 178 |
DRGN_DEBUG_INFO_OPTIONS_GET(const char * const *, name) \ |
| 179 |
\ |
| 180 |
LIBDRGN_PUBLIC struct drgn_error * \ |
| 181 |
drgn_debug_info_options_set_##name(struct drgn_debug_info_options *options, \ |
| 182 |
const char * const *value) \ |
| 183 |
{ \ |
| 184 |
struct drgn_error *err; \ |
| 185 |
const char * const *copy; \ |
| 186 |
if (value == drgn_debug_info_options_default_##name) { \ |
| 187 |
copy = value; \ |
| 188 |
} else { \ |
| 189 |
err = drgn_debug_info_options_list_dup(value, \ |
| 190 |
drgn_debug_info_options_##name##_allow_empty,\ |
| 191 |
©); \ |
| 192 |
if (err) \ |
| 193 |
return err; \ |
| 194 |
} \ |
| 195 |
drgn_debug_info_options_list_destroy(options->name, \ |
| 196 |
drgn_debug_info_options_default_##name);\ |
| 197 |
options->name = copy; \ |
| 198 |
return NULL; \ |
| 199 |
} |
| 200 |
|
| 201 |
#define BOOL_OPTION(name, default_value) \ |
| 202 |
DRGN_DEBUG_INFO_OPTIONS_GETSET(bool, name) |
| 203 |
#define ENUM_OPTION(name, type, default_value) \ |
| 204 |
DRGN_DEBUG_INFO_OPTIONS_GETSET(enum type, name) |
| 205 |
|
| 206 |
DRGN_DEBUG_INFO_OPTIONS |
| 207 |
|
| 208 |
#undef ENUM_OPTION |
| 209 |
#undef BOOL_OPTION |
| 210 |
#undef LIST_OPTION |
| 211 |
|
| 212 |
static bool drgn_format_debug_info_options_common(struct string_builder *sb, |
| 213 |
const char *name, |
| 214 |
bool *first) |
| 215 |
{ |
| 216 |
if (*first) |
| 217 |
*first = false; |
| 218 |
else if (!string_builder_append(sb, ", ")) |
| 219 |
return false; |
| 220 |
return string_builder_append(sb, name) && string_builder_appendc(sb, '='); |
| 221 |
} |
| 222 |
|
| 223 |
static bool drgn_debug_info_options_lists_equal(const char * const *a, |
| 224 |
const char * const *b) |
| 225 |
{ |
| 226 |
if (a == b) |
| 227 |
return true; |
| 228 |
size_t i; |
| 229 |
for (i = 0; a[i]; i++) { |
| 230 |
if (!b[i] || strcmp(a[i], b[i]) != 0) |
| 231 |
return false; |
| 232 |
} |
| 233 |
return !b[i]; |
| 234 |
} |
| 235 |
|
| 236 |
static bool drgn_format_debug_info_options_list(struct string_builder *sb, |
| 237 |
const char *name, bool *first, |
| 238 |
const char * const *list, |
| 239 |
const char * const *default_list) |
| 240 |
{ |
| 241 |
// Always include directories, skip other options set to the default. |
| 242 |
if (default_list != drgn_debug_info_options_default_directories |
| 243 |
&& drgn_debug_info_options_lists_equal(list, default_list)) |
| 244 |
return true; |
| 245 |
|
| 246 |
if (!drgn_format_debug_info_options_common(sb, name, first) |
| 247 |
|| !string_builder_appendc(sb, '(')) |
| 248 |
return false; |
| 249 |
size_t i; |
| 250 |
for (i = 0; list[i]; i++) { |
| 251 |
if (!string_builder_append(sb, i == 0 ? "'" : ", '") |
| 252 |
|| !string_builder_append(sb, list[i]) |
| 253 |
|| !string_builder_appendc(sb, '\'')) |
| 254 |
return false; |
| 255 |
} |
| 256 |
return string_builder_append(sb, i == 1 ? ",)" : ")"); |
| 257 |
} |
| 258 |
|
| 259 |
static bool drgn_format_debug_info_options_bool(struct string_builder *sb, |
| 260 |
const char *name, bool *first, |
| 261 |
bool value, bool default_value) |
| 262 |
{ |
| 263 |
// Skip options set to the default. |
| 264 |
if (value == default_value) |
| 265 |
return true; |
| 266 |
return drgn_format_debug_info_options_common(sb, name, first) |
| 267 |
&& string_builder_append(sb, value ? "True" : "False"); |
| 268 |
} |
| 269 |
|
| 270 |
static bool |
| 271 |
drgn_kmod_search_method_format(struct string_builder *sb, const char *name, |
| 272 |
bool *first, enum drgn_kmod_search_method value, |
| 273 |
enum drgn_kmod_search_method default_value) |
| 274 |
{ |
| 275 |
// Skip options set to the default. |
| 276 |
if (value == default_value) |
| 277 |
return true; |
| 278 |
const char *s; |
| 279 |
SWITCH_ENUM(value) { |
| 280 |
case DRGN_KMOD_SEARCH_NONE: |
| 281 |
s = "NONE"; |
| 282 |
break; |
| 283 |
case DRGN_KMOD_SEARCH_DEPMOD: |
| 284 |
s = "DEPMOD"; |
| 285 |
break; |
| 286 |
case DRGN_KMOD_SEARCH_WALK: |
| 287 |
s = "WALK"; |
| 288 |
break; |
| 289 |
case DRGN_KMOD_SEARCH_DEPMOD_OR_WALK: |
| 290 |
s = "DEPMOD_OR_WALK"; |
| 291 |
break; |
| 292 |
case DRGN_KMOD_SEARCH_DEPMOD_AND_WALK: |
| 293 |
s = "DEPMOD_AND_WALK"; |
| 294 |
break; |
| 295 |
default: |
| 296 |
UNREACHABLE(); |
| 297 |
} |
| 298 |
return drgn_format_debug_info_options_common(sb, name, first) |
| 299 |
&& string_builder_append(sb, s); |
| 300 |
} |
| 301 |
|
| 302 |
char *drgn_format_debug_info_options(struct drgn_debug_info_options *options) |
| 303 |
{ |
| 304 |
STRING_BUILDER(sb); |
| 305 |
|
| 306 |
bool first = true; |
| 307 |
#define LIST_OPTION(name) \ |
| 308 |
if (!drgn_format_debug_info_options_list(&sb, #name, &first, \ |
| 309 |
options->name, \ |
| 310 |
drgn_debug_info_options_default_##name))\ |
| 311 |
return NULL; |
| 312 |
#define BOOL_OPTION(name, default_value) \ |
| 313 |
if (!drgn_format_debug_info_options_bool(&sb, #name, &first, \ |
| 314 |
options->name, default_value)) \ |
| 315 |
return NULL; |
| 316 |
#define ENUM_OPTION(name, type, default_value) \ |
| 317 |
if (!type##_format(&sb, #name, &first, options->name, default_value)) \ |
| 318 |
return NULL; |
| 319 |
DRGN_DEBUG_INFO_OPTIONS |
| 320 |
#undef ENUM_OPTION |
| 321 |
#undef BOOL_OPTION |
| 322 |
#undef LIST_OPTION |
| 323 |
if (!string_builder_null_terminate(&sb)) |
| 324 |
return NULL; |
| 325 |
return string_builder_steal(&sb); |
| 326 |
} |