Debian Codegraph @

libdrgn/handler.c

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