1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <string.h>
#include "cleanup.h"
#include "drgn_internal.h"
#include "handler.h"
#include "hash_table.h"
#include "util.h"
struct drgn_error *drgn_handler_list_register(struct drgn_handler_list *list,
struct drgn_handler *new_handler,
size_t enable_idx,
const char *what)
{
struct drgn_handler **insert_pos = &list->head;
size_t num_enabled = 0;
for (struct drgn_handler *handler = list->head; handler;
handler = handler->next) {
if (strcmp(new_handler->name, handler->name) == 0) {
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
"duplicate %s name '%s'",
what, handler->name);
}
if (handler->enabled && num_enabled < enable_idx) {
insert_pos = &handler->next;
num_enabled++;
}
}
new_handler->enabled = enable_idx != DRGN_HANDLER_REGISTER_DONT_ENABLE;
new_handler->next = *insert_pos;
*insert_pos = new_handler;
return NULL;
}
#define drgn_handler_list_for_each_registered(handler, list) \
for (struct drgn_handler *handler = (list)->head; handler; \
handler = handler->next)
struct drgn_error *drgn_handler_list_registered(struct drgn_handler_list *list,
const char ***names_ret,
size_t *count_ret)
{
size_t n = 0;
drgn_handler_list_for_each_registered(handler, list)
n++;
const char **names = malloc_array(n, sizeof(names[0]));
if (!names)
return &drgn_enomem;
size_t i = 0;
drgn_handler_list_for_each_registered(handler, list)
names[i++] = handler->name;
*names_ret = names;
*count_ret = n;
return NULL;
}
static inline const char *drgn_handler_entry_to_key(const uintptr_t *entry)
{
return ((struct drgn_handler *)(*entry & ~1))->name;
}
DEFINE_HASH_TABLE(drgn_handler_table, uintptr_t, drgn_handler_entry_to_key,
c_string_key_hash_pair, c_string_key_eq);
struct drgn_error *drgn_handler_list_set_enabled(struct drgn_handler_list *list,
const char * const *names,
size_t count, const char *what)
{
// Put all of the handlers in a hash table of tagged pointers.
HASH_TABLE(drgn_handler_table, table);
drgn_handler_list_for_each_registered(handler, list) {
uintptr_t entry = (uintptr_t)handler;
if (drgn_handler_table_insert(&table, &entry, NULL) < 0)
return &drgn_enomem;
}
// Check the list of names.
for (size_t i = 0; i < count; i++) {
auto it = drgn_handler_table_search(&table, &names[i]);
if (!it.entry) {
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
"%s '%s' not found", what,
names[i]);
}
if (*it.entry & 1) {
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
"%s '%s' enabled multiple times",
what, names[i]);
}
*it.entry |= 1;
}
// Insert the enabled handlers and delete them from the hash table.
struct drgn_handler **handlerp = &list->head;
for (size_t i = 0; i < count; i++) {
auto it = drgn_handler_table_search(&table, &names[i]);
struct drgn_handler *handler =
(struct drgn_handler *)(*it.entry & ~1);
handler->enabled = true;
*handlerp = handler;
handlerp = &handler->next;
drgn_handler_table_delete_iterator(&table, it);
}
// The remaining handlers in the hash table are disabled. Insert them.
hash_table_for_each(drgn_handler_table, it, &table) {
struct drgn_handler *handler = (struct drgn_handler *)*it.entry;
handler->enabled = false;
*handlerp = handler;
handlerp = &handler->next;
}
*handlerp = NULL;
return NULL;
}
struct drgn_error *drgn_handler_list_enabled(struct drgn_handler_list *list,
const char ***names_ret,
size_t *count_ret)
{
size_t n = 0;
drgn_handler_list_for_each_enabled(struct drgn_handler, handler, list)
n++;
const char **names = malloc_array(n, sizeof(names[0]));
if (!names)
return &drgn_enomem;
size_t i = 0;
drgn_handler_list_for_each_enabled(struct drgn_handler, handler, list)
names[i++] = handler->name;
*names_ret = names;
*count_ret = n;
return NULL;
}
bool drgn_handler_list_disable(struct drgn_handler_list *list,
const char *name)
{
// Find an enabled handler with the given name.
struct drgn_handler **handlerp = &list->head;
struct drgn_handler *handler = list->head;
for (;;) {
if (!handler || !handler->enabled)
return false;
if (strcmp(handler->name, name) == 0)
break;
handlerp = &handler->next;
handler = handler->next;
}
// Disable the handler.
handler->enabled = false;
// Move it to the appropriate part of the list (after all enabled
// handlers).
*handlerp = handler->next;
while (*handlerp && (*handlerp)->enabled)
handlerp = &(*handlerp)->next;
handler->next = *handlerp;
*handlerp = handler;
return true;
}
|