Revert "Remove useless const qualifier"
[lttng-ust.git] / liblttng-ust / ltt-probes.c
1 /*
2 * ltt-probes.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng probes registry.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11 #include <string.h>
12 #include <errno.h>
13 #include <urcu/list.h>
14 #include <urcu/hlist.h>
15 #include <lttng/ust-events.h>
16 #include <lttng/tracepoint.h>
17 #include "tracepoint-internal.h"
18 #include <assert.h>
19 #include <helper.h>
20 #include <ctype.h>
21
22 #include "ltt-tracer-core.h"
23 #include "jhash.h"
24 #include "error.h"
25
26 /*
27 * probe list is protected by ust_lock()/ust_unlock().
28 */
29 static CDS_LIST_HEAD(probe_list);
30
31 static
32 const struct lttng_probe_desc *find_provider(const char *provider)
33 {
34 struct lttng_probe_desc *iter;
35
36 cds_list_for_each_entry(iter, &probe_list, head) {
37 if (!strcmp(iter->provider, provider))
38 return iter;
39 }
40 return NULL;
41 }
42
43 static
44 const struct lttng_event_desc *find_event(const char *name)
45 {
46 struct lttng_probe_desc *probe_desc;
47 int i;
48
49 cds_list_for_each_entry(probe_desc, &probe_list, head) {
50 for (i = 0; i < probe_desc->nr_events; i++) {
51 if (!strncmp(probe_desc->event_desc[i]->name, name,
52 LTTNG_UST_SYM_NAME_LEN - 1))
53 return probe_desc->event_desc[i];
54 }
55 }
56 return NULL;
57 }
58
59 int ltt_probe_register(struct lttng_probe_desc *desc)
60 {
61 struct lttng_probe_desc *iter;
62 int ret = 0;
63 int i;
64
65 ust_lock();
66 if (find_provider(desc->provider)) {
67 ret = -EEXIST;
68 goto end;
69 }
70 /*
71 * TODO: This is O(N^2). Turn into a hash table when probe registration
72 * overhead becomes an issue.
73 */
74 for (i = 0; i < desc->nr_events; i++) {
75 if (find_event(desc->event_desc[i]->name)) {
76 ret = -EEXIST;
77 goto end;
78 }
79 }
80
81 /*
82 * We sort the providers by struct lttng_probe_desc pointer
83 * address.
84 */
85 cds_list_for_each_entry_reverse(iter, &probe_list, head) {
86 BUG_ON(iter == desc); /* Should never be in the list twice */
87 if (iter < desc) {
88 /* We belong to the location right after iter. */
89 cds_list_add(&desc->head, &iter->head);
90 goto desc_added;
91 }
92 }
93 /* We should be added at the head of the list */
94 cds_list_add(&desc->head, &probe_list);
95 desc_added:
96 DBG("just registered probe %s containing %u events",
97 desc->provider, desc->nr_events);
98 /*
99 * fix the events awaiting probe load.
100 */
101 for (i = 0; i < desc->nr_events; i++) {
102 ret = pending_probe_fix_events(desc->event_desc[i]);
103 assert(!ret);
104 }
105 end:
106 ust_unlock();
107 return ret;
108 }
109
110 void ltt_probe_unregister(struct lttng_probe_desc *desc)
111 {
112 ust_lock();
113 cds_list_del(&desc->head);
114 DBG("just unregistered probe %s", desc->provider);
115 ust_unlock();
116 }
117
118 /*
119 * called with UST lock held.
120 */
121 const struct lttng_event_desc *ltt_event_get(const char *name)
122 {
123 const struct lttng_event_desc *event;
124
125 event = find_event(name);
126 if (!event)
127 return NULL;
128 return event;
129 }
130
131 void ltt_event_put(const struct lttng_event_desc *event)
132 {
133 }
134
135 void ltt_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
136 {
137 struct tp_list_entry *list_entry, *tmp;
138
139 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
140 cds_list_del(&list_entry->head);
141 free(list_entry);
142 }
143 }
144
145 /*
146 * called with UST lock held.
147 */
148 int ltt_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
149 {
150 struct lttng_probe_desc *probe_desc;
151 int i;
152
153 CDS_INIT_LIST_HEAD(&list->head);
154 cds_list_for_each_entry(probe_desc, &probe_list, head) {
155 for (i = 0; i < probe_desc->nr_events; i++) {
156 struct tp_list_entry *list_entry;
157
158 list_entry = zmalloc(sizeof(*list_entry));
159 if (!list_entry)
160 goto err_nomem;
161 cds_list_add(&list_entry->head, &list->head);
162 strncpy(list_entry->tp.name,
163 probe_desc->event_desc[i]->name,
164 LTTNG_UST_SYM_NAME_LEN);
165 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
166 if (!probe_desc->event_desc[i]->loglevel) {
167 list_entry->tp.loglevel = TRACE_DEFAULT;
168 } else {
169 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
170 }
171 }
172 }
173 if (cds_list_empty(&list->head))
174 list->iter = NULL;
175 else
176 list->iter =
177 cds_list_first_entry(&list->head, struct tp_list_entry, head);
178 return 0;
179
180 err_nomem:
181 ltt_probes_prune_event_list(list);
182 return -ENOMEM;
183 }
184
185 /*
186 * Return current iteration position, advance internal iterator to next.
187 * Return NULL if end of list.
188 */
189 struct lttng_ust_tracepoint_iter *
190 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
191 {
192 struct tp_list_entry *entry;
193
194 if (!list->iter)
195 return NULL;
196 entry = list->iter;
197 if (entry->head.next == &list->head)
198 list->iter = NULL;
199 else
200 list->iter = cds_list_entry(entry->head.next,
201 struct tp_list_entry, head);
202 return &entry->tp;
203 }
204
205 /*
206 * marshall all probes/all events and create those that fit the
207 * wildcard. Add them to the events list as created.
208 */
209 void ltt_probes_create_wildcard_events(struct wildcard_entry *entry,
210 struct session_wildcard *wildcard)
211 {
212 struct lttng_probe_desc *probe_desc;
213 struct lttng_ust_event event_param;
214 int i;
215
216 cds_list_for_each_entry(probe_desc, &probe_list, head) {
217 for (i = 0; i < probe_desc->nr_events; i++) {
218 const struct lttng_event_desc *event_desc;
219 int match = 0;
220
221 event_desc = probe_desc->event_desc[i];
222 /* compare excluding final '*' */
223 assert(strlen(entry->name) > 0);
224 if (strcmp(event_desc->name, "lttng_ust:metadata")
225 && (strlen(entry->name) == 1
226 || !strncmp(event_desc->name, entry->name,
227 strlen(entry->name) - 1))) {
228 if (ltt_loglevel_match(event_desc,
229 entry->loglevel_type,
230 entry->loglevel)) {
231 match = 1;
232 }
233 }
234 if (match) {
235 struct ltt_event *ev;
236 int ret;
237
238 memcpy(&event_param, &wildcard->event_param,
239 sizeof(event_param));
240 memcpy(event_param.name,
241 event_desc->name,
242 sizeof(event_param.name));
243 /* create event */
244 ret = ltt_event_create(wildcard->chan,
245 &event_param, NULL,
246 &ev);
247 if (ret) {
248 DBG("Error creating event");
249 continue;
250 }
251 cds_list_add(&ev->wildcard_list,
252 &wildcard->events);
253 }
254 }
255 }
256 }
257
This page took 0.035401 seconds and 4 git commands to generate.