Rename libustd to libustconsumer and ustd to ust-consumerd
[lttng-ust.git] / libust / marker.c
1 /*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdlib.h>
20 #include <errno.h>
21 #define _LGPL_SOURCE
22 #include <urcu-bp.h>
23 #include <urcu/rculist.h>
24 #include <urcu/hlist.h>
25
26 #include <ust/core.h>
27 #include <ust/marker.h>
28
29 #include "usterr.h"
30 #include "channels.h"
31 #include "tracercore.h"
32 #include "tracer.h"
33
34 __thread long ust_reg_stack[500];
35 volatile __thread long *ust_reg_stack_ptr = (long *) 0;
36
37 extern struct marker __start___markers[] __attribute__((visibility("hidden")));
38 extern struct marker __stop___markers[] __attribute__((visibility("hidden")));
39
40 /* Set to 1 to enable marker debug output */
41 static const int marker_debug;
42
43 /*
44 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
45 * and module markers and the hash table.
46 */
47 static DEFINE_MUTEX(markers_mutex);
48
49 static CDS_LIST_HEAD(libs);
50
51
52 void lock_markers(void)
53 {
54 pthread_mutex_lock(&markers_mutex);
55 }
56
57 void unlock_markers(void)
58 {
59 pthread_mutex_unlock(&markers_mutex);
60 }
61
62 /*
63 * Marker hash table, containing the active markers.
64 * Protected by module_mutex.
65 */
66 #define MARKER_HASH_BITS 6
67 #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
68 static struct cds_hlist_head marker_table[MARKER_TABLE_SIZE];
69
70 /*
71 * Note about RCU :
72 * It is used to make sure every handler has finished using its private data
73 * between two consecutive operation (add or remove) on a given marker. It is
74 * also used to delay the free of multiple probes array until a quiescent state
75 * is reached.
76 * marker entries modifications are protected by the markers_mutex.
77 */
78 struct marker_entry {
79 struct cds_hlist_node hlist;
80 char *format;
81 char *name;
82 /* Probe wrapper */
83 void (*call)(const struct marker *mdata, void *call_private, struct registers *regs, ...);
84 struct marker_probe_closure single;
85 struct marker_probe_closure *multi;
86 int refcount; /* Number of times armed. 0 if disarmed. */
87 struct rcu_head rcu;
88 void *oldptr;
89 int rcu_pending;
90 u16 channel_id;
91 u16 event_id;
92 unsigned char ptype:1;
93 unsigned char format_allocated:1;
94 char channel[0]; /* Contains channel'\0'name'\0'format'\0' */
95 };
96
97 #ifdef CONFIG_MARKERS_USERSPACE
98 static void marker_update_processes(void);
99 #else
100 static void marker_update_processes(void)
101 {
102 }
103 #endif
104
105 /**
106 * __mark_empty_function - Empty probe callback
107 * @mdata: marker data
108 * @probe_private: probe private data
109 * @call_private: call site private data
110 * @fmt: format string
111 * @...: variable argument list
112 *
113 * Empty callback provided as a probe to the markers. By providing this to a
114 * disabled marker, we make sure the execution flow is always valid even
115 * though the function pointer change and the marker enabling are two distinct
116 * operations that modifies the execution flow of preemptible code.
117 */
118 notrace void __mark_empty_function(const struct marker *mdata,
119 void *probe_private, struct registers *regs, void *call_private, const char *fmt, va_list *args)
120 {
121 }
122 //ust// EXPORT_SYMBOL_GPL(__mark_empty_function);
123
124 /*
125 * marker_probe_cb Callback that prepares the variable argument list for probes.
126 * @mdata: pointer of type struct marker
127 * @call_private: caller site private data
128 * @...: Variable argument list.
129 *
130 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
131 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
132 * rcu_dereference() for the pointer read.
133 */
134 notrace void marker_probe_cb(const struct marker *mdata,
135 void *call_private, struct registers *regs, ...)
136 {
137 va_list args;
138 char ptype;
139
140 /*
141 * rcu_read_lock_sched does two things : disabling preemption to make
142 * sure the teardown of the callbacks can be done correctly when they
143 * are in modules and they insure RCU read coherency.
144 */
145 //ust// rcu_read_lock_sched_notrace();
146 ptype = mdata->ptype;
147 if (likely(!ptype)) {
148 marker_probe_func *func;
149 /* Must read the ptype before ptr. They are not data dependant,
150 * so we put an explicit cmm_smp_rmb() here. */
151 cmm_smp_rmb();
152 func = mdata->single.func;
153 /* Must read the ptr before private data. They are not data
154 * dependant, so we put an explicit cmm_smp_rmb() here. */
155 cmm_smp_rmb();
156 va_start(args, regs);
157 func(mdata, mdata->single.probe_private, regs, call_private,
158 mdata->format, &args);
159 va_end(args);
160 } else {
161 struct marker_probe_closure *multi;
162 int i;
163 /*
164 * Read mdata->ptype before mdata->multi.
165 */
166 cmm_smp_rmb();
167 multi = mdata->multi;
168 /*
169 * multi points to an array, therefore accessing the array
170 * depends on reading multi. However, even in this case,
171 * we must insure that the pointer is read _before_ the array
172 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
173 * in the fast path, so put the explicit cmm_barrier here.
174 */
175 cmm_smp_read_barrier_depends();
176 for (i = 0; multi[i].func; i++) {
177 va_start(args, regs);
178 multi[i].func(mdata, multi[i].probe_private,
179 regs, call_private, mdata->format, &args);
180 va_end(args);
181 }
182 }
183 //ust// rcu_read_unlock_sched_notrace();
184 }
185 //ust// EXPORT_SYMBOL_GPL(marker_probe_cb);
186
187 /*
188 * marker_probe_cb Callback that does not prepare the variable argument list.
189 * @mdata: pointer of type struct marker
190 * @call_private: caller site private data
191 * @...: Variable argument list.
192 *
193 * Should be connected to markers "MARK_NOARGS".
194 */
195 static notrace void marker_probe_cb_noarg(const struct marker *mdata,
196 void *call_private, struct registers *regs, ...)
197 {
198 va_list args; /* not initialized */
199 char ptype;
200
201 //ust// rcu_read_lock_sched_notrace();
202 ptype = mdata->ptype;
203 if (likely(!ptype)) {
204 marker_probe_func *func;
205 /* Must read the ptype before ptr. They are not data dependant,
206 * so we put an explicit cmm_smp_rmb() here. */
207 cmm_smp_rmb();
208 func = mdata->single.func;
209 /* Must read the ptr before private data. They are not data
210 * dependant, so we put an explicit cmm_smp_rmb() here. */
211 cmm_smp_rmb();
212 func(mdata, mdata->single.probe_private, regs, call_private,
213 mdata->format, &args);
214 } else {
215 struct marker_probe_closure *multi;
216 int i;
217 /*
218 * Read mdata->ptype before mdata->multi.
219 */
220 cmm_smp_rmb();
221 multi = mdata->multi;
222 /*
223 * multi points to an array, therefore accessing the array
224 * depends on reading multi. However, even in this case,
225 * we must insure that the pointer is read _before_ the array
226 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
227 * in the fast path, so put the explicit cmm_barrier here.
228 */
229 cmm_smp_read_barrier_depends();
230 for (i = 0; multi[i].func; i++)
231 multi[i].func(mdata, multi[i].probe_private, regs,
232 call_private, mdata->format, &args);
233 }
234 //ust// rcu_read_unlock_sched_notrace();
235 }
236
237 static void free_old_closure(struct rcu_head *head)
238 {
239 struct marker_entry *entry = _ust_container_of(head,
240 struct marker_entry, rcu);
241 free(entry->oldptr);
242 /* Make sure we free the data before setting the pending flag to 0 */
243 cmm_smp_wmb();
244 entry->rcu_pending = 0;
245 }
246
247 static void debug_print_probes(struct marker_entry *entry)
248 {
249 int i;
250
251 if (!marker_debug)
252 return;
253
254 if (!entry->ptype) {
255 DBG("Single probe : %p %p",
256 entry->single.func,
257 entry->single.probe_private);
258 } else {
259 for (i = 0; entry->multi[i].func; i++)
260 DBG("Multi probe %d : %p %p", i,
261 entry->multi[i].func,
262 entry->multi[i].probe_private);
263 }
264 }
265
266 static struct marker_probe_closure *
267 marker_entry_add_probe(struct marker_entry *entry,
268 marker_probe_func *probe, void *probe_private)
269 {
270 int nr_probes = 0;
271 struct marker_probe_closure *old, *new;
272
273 WARN_ON(!probe);
274
275 debug_print_probes(entry);
276 old = entry->multi;
277 if (!entry->ptype) {
278 if (entry->single.func == probe &&
279 entry->single.probe_private == probe_private)
280 return ERR_PTR(-EBUSY);
281 if (entry->single.func == __mark_empty_function) {
282 /* 0 -> 1 probes */
283 entry->single.func = probe;
284 entry->single.probe_private = probe_private;
285 entry->refcount = 1;
286 entry->ptype = 0;
287 debug_print_probes(entry);
288 return NULL;
289 } else {
290 /* 1 -> 2 probes */
291 nr_probes = 1;
292 old = NULL;
293 }
294 } else {
295 /* (N -> N+1), (N != 0, 1) probes */
296 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
297 if (old[nr_probes].func == probe
298 && old[nr_probes].probe_private
299 == probe_private)
300 return ERR_PTR(-EBUSY);
301 }
302 /* + 2 : one for new probe, one for NULL func */
303 new = zmalloc((nr_probes + 2) * sizeof(struct marker_probe_closure));
304 if (new == NULL)
305 return ERR_PTR(-ENOMEM);
306 if (!old)
307 new[0] = entry->single;
308 else
309 memcpy(new, old,
310 nr_probes * sizeof(struct marker_probe_closure));
311 new[nr_probes].func = probe;
312 new[nr_probes].probe_private = probe_private;
313 entry->refcount = nr_probes + 1;
314 entry->multi = new;
315 entry->ptype = 1;
316 debug_print_probes(entry);
317 return old;
318 }
319
320 static struct marker_probe_closure *
321 marker_entry_remove_probe(struct marker_entry *entry,
322 marker_probe_func *probe, void *probe_private)
323 {
324 int nr_probes = 0, nr_del = 0, i;
325 struct marker_probe_closure *old, *new;
326
327 old = entry->multi;
328
329 debug_print_probes(entry);
330 if (!entry->ptype) {
331 /* 0 -> N is an error */
332 WARN_ON(entry->single.func == __mark_empty_function);
333 /* 1 -> 0 probes */
334 WARN_ON(probe && entry->single.func != probe);
335 WARN_ON(entry->single.probe_private != probe_private);
336 entry->single.func = __mark_empty_function;
337 entry->refcount = 0;
338 entry->ptype = 0;
339 debug_print_probes(entry);
340 return NULL;
341 } else {
342 /* (N -> M), (N > 1, M >= 0) probes */
343 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
344 if ((!probe || old[nr_probes].func == probe)
345 && old[nr_probes].probe_private
346 == probe_private)
347 nr_del++;
348 }
349 }
350
351 if (nr_probes - nr_del == 0) {
352 /* N -> 0, (N > 1) */
353 entry->single.func = __mark_empty_function;
354 entry->refcount = 0;
355 entry->ptype = 0;
356 } else if (nr_probes - nr_del == 1) {
357 /* N -> 1, (N > 1) */
358 for (i = 0; old[i].func; i++)
359 if ((probe && old[i].func != probe) ||
360 old[i].probe_private != probe_private)
361 entry->single = old[i];
362 entry->refcount = 1;
363 entry->ptype = 0;
364 } else {
365 int j = 0;
366 /* N -> M, (N > 1, M > 1) */
367 /* + 1 for NULL */
368 new = zmalloc((nr_probes - nr_del + 1) * sizeof(struct marker_probe_closure));
369 if (new == NULL)
370 return ERR_PTR(-ENOMEM);
371 for (i = 0; old[i].func; i++)
372 if ((probe && old[i].func != probe) ||
373 old[i].probe_private != probe_private)
374 new[j++] = old[i];
375 entry->refcount = nr_probes - nr_del;
376 entry->ptype = 1;
377 entry->multi = new;
378 }
379 debug_print_probes(entry);
380 return old;
381 }
382
383 /*
384 * Get marker if the marker is present in the marker hash table.
385 * Must be called with markers_mutex held.
386 * Returns NULL if not present.
387 */
388 static struct marker_entry *get_marker(const char *channel, const char *name)
389 {
390 struct cds_hlist_head *head;
391 struct cds_hlist_node *node;
392 struct marker_entry *e;
393 size_t channel_len = strlen(channel) + 1;
394 size_t name_len = strlen(name) + 1;
395 u32 hash;
396
397 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
398 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
399 cds_hlist_for_each_entry(e, node, head, hlist) {
400 if (!strcmp(channel, e->channel) && !strcmp(name, e->name))
401 return e;
402 }
403 return NULL;
404 }
405
406 /*
407 * Add the marker to the marker hash table. Must be called with markers_mutex
408 * held.
409 */
410 static struct marker_entry *add_marker(const char *channel, const char *name,
411 const char *format)
412 {
413 struct cds_hlist_head *head;
414 struct cds_hlist_node *node;
415 struct marker_entry *e;
416 size_t channel_len = strlen(channel) + 1;
417 size_t name_len = strlen(name) + 1;
418 size_t format_len = 0;
419 u32 hash;
420
421 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
422 if (format)
423 format_len = strlen(format) + 1;
424 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
425 cds_hlist_for_each_entry(e, node, head, hlist) {
426 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
427 DBG("Marker %s.%s busy", channel, name);
428 return ERR_PTR(-EBUSY); /* Already there */
429 }
430 }
431 /*
432 * Using zmalloc here to allocate a variable length element. Could
433 * cause some memory fragmentation if overused.
434 */
435 e = zmalloc(sizeof(struct marker_entry)
436 + channel_len + name_len + format_len);
437 if (!e)
438 return ERR_PTR(-ENOMEM);
439 memcpy(e->channel, channel, channel_len);
440 e->name = &e->channel[channel_len];
441 memcpy(e->name, name, name_len);
442 if (format) {
443 e->format = &e->name[name_len];
444 memcpy(e->format, format, format_len);
445 if (strcmp(e->format, MARK_NOARGS) == 0)
446 e->call = marker_probe_cb_noarg;
447 else
448 e->call = marker_probe_cb;
449 trace_mark(metadata, core_marker_format,
450 "channel %s name %s format %s",
451 e->channel, e->name, e->format);
452 } else {
453 e->format = NULL;
454 e->call = marker_probe_cb;
455 }
456 e->single.func = __mark_empty_function;
457 e->single.probe_private = NULL;
458 e->multi = NULL;
459 e->ptype = 0;
460 e->format_allocated = 0;
461 e->refcount = 0;
462 e->rcu_pending = 0;
463 cds_hlist_add_head(&e->hlist, head);
464 return e;
465 }
466
467 /*
468 * Remove the marker from the marker hash table. Must be called with mutex_lock
469 * held.
470 */
471 static int remove_marker(const char *channel, const char *name)
472 {
473 struct cds_hlist_head *head;
474 struct cds_hlist_node *node;
475 struct marker_entry *e;
476 int found = 0;
477 size_t channel_len = strlen(channel) + 1;
478 size_t name_len = strlen(name) + 1;
479 u32 hash;
480 int ret;
481
482 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
483 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
484 cds_hlist_for_each_entry(e, node, head, hlist) {
485 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
486 found = 1;
487 break;
488 }
489 }
490 if (!found)
491 return -ENOENT;
492 if (e->single.func != __mark_empty_function)
493 return -EBUSY;
494 cds_hlist_del(&e->hlist);
495 if (e->format_allocated)
496 free(e->format);
497 ret = ltt_channels_unregister(e->channel);
498 WARN_ON(ret);
499 /* Make sure the call_rcu has been executed */
500 //ust// if (e->rcu_pending)
501 //ust// rcu_cmm_barrier_sched();
502 free(e);
503 return 0;
504 }
505
506 /*
507 * Set the mark_entry format to the format found in the element.
508 */
509 static int marker_set_format(struct marker_entry *entry, const char *format)
510 {
511 entry->format = strdup(format);
512 if (!entry->format)
513 return -ENOMEM;
514 entry->format_allocated = 1;
515
516 trace_mark(metadata, core_marker_format,
517 "channel %s name %s format %s",
518 entry->channel, entry->name, entry->format);
519 return 0;
520 }
521
522 /*
523 * Sets the probe callback corresponding to one marker.
524 */
525 static int set_marker(struct marker_entry *entry, struct marker *elem,
526 int active)
527 {
528 int ret = 0;
529 WARN_ON(strcmp(entry->name, elem->name) != 0);
530
531 if (entry->format) {
532 if (strcmp(entry->format, elem->format) != 0) {
533 ERR("Format mismatch for probe %s (%s), marker (%s)",
534 entry->name,
535 entry->format,
536 elem->format);
537 return -EPERM;
538 }
539 } else {
540 ret = marker_set_format(entry, elem->format);
541 if (ret)
542 return ret;
543 }
544
545 /*
546 * probe_cb setup (statically known) is done here. It is
547 * asynchronous with the rest of execution, therefore we only
548 * pass from a "safe" callback (with argument) to an "unsafe"
549 * callback (does not set arguments).
550 */
551 elem->call = entry->call;
552 elem->channel_id = entry->channel_id;
553 elem->event_id = entry->event_id;
554 /*
555 * Sanity check :
556 * We only update the single probe private data when the ptr is
557 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
558 */
559 WARN_ON(elem->single.func != __mark_empty_function
560 && elem->single.probe_private != entry->single.probe_private
561 && !elem->ptype);
562 elem->single.probe_private = entry->single.probe_private;
563 /*
564 * Make sure the private data is valid when we update the
565 * single probe ptr.
566 */
567 cmm_smp_wmb();
568 elem->single.func = entry->single.func;
569 /*
570 * We also make sure that the new probe callbacks array is consistent
571 * before setting a pointer to it.
572 */
573 rcu_assign_pointer(elem->multi, entry->multi);
574 /*
575 * Update the function or multi probe array pointer before setting the
576 * ptype.
577 */
578 cmm_smp_wmb();
579 elem->ptype = entry->ptype;
580
581 if (elem->tp_name && (active ^ _imv_read(elem->state))) {
582 WARN_ON(!elem->tp_cb);
583 /*
584 * It is ok to directly call the probe registration because type
585 * checking has been done in the __trace_mark_tp() macro.
586 */
587
588 if (active) {
589 /*
590 * try_module_get should always succeed because we hold
591 * markers_mutex to get the tp_cb address.
592 */
593 //ust// ret = try_module_get(__module_text_address(
594 //ust// (unsigned long)elem->tp_cb));
595 //ust// BUG_ON(!ret);
596 ret = tracepoint_probe_register_noupdate(
597 elem->tp_name,
598 elem->tp_cb, NULL);
599 } else {
600 ret = tracepoint_probe_unregister_noupdate(
601 elem->tp_name,
602 elem->tp_cb, NULL);
603 /*
604 * tracepoint_probe_update_all() must be called
605 * before the module containing tp_cb is unloaded.
606 */
607 //ust// module_put(__module_text_address(
608 //ust// (unsigned long)elem->tp_cb));
609 }
610 }
611 elem->state__imv = active;
612
613 return ret;
614 }
615
616 /*
617 * Disable a marker and its probe callback.
618 * Note: only waiting an RCU period after setting elem->call to the empty
619 * function insures that the original callback is not used anymore. This insured
620 * by rcu_read_lock_sched around the call site.
621 */
622 static void disable_marker(struct marker *elem)
623 {
624 int ret;
625
626 /* leave "call" as is. It is known statically. */
627 if (elem->tp_name && _imv_read(elem->state)) {
628 WARN_ON(!elem->tp_cb);
629 /*
630 * It is ok to directly call the probe registration because type
631 * checking has been done in the __trace_mark_tp() macro.
632 */
633 ret = tracepoint_probe_unregister_noupdate(elem->tp_name,
634 elem->tp_cb, NULL);
635 WARN_ON(ret);
636 /*
637 * tracepoint_probe_update_all() must be called
638 * before the module containing tp_cb is unloaded.
639 */
640 //ust// module_put(__module_text_address((unsigned long)elem->tp_cb));
641 }
642 elem->state__imv = 0;
643 elem->single.func = __mark_empty_function;
644 /* Update the function before setting the ptype */
645 cmm_smp_wmb();
646 elem->ptype = 0; /* single probe */
647 /*
648 * Leave the private data and channel_id/event_id there, because removal
649 * is racy and should be done only after an RCU period. These are never
650 * used until the next initialization anyway.
651 */
652 }
653
654 /*
655 * is_marker_enabled - Check if a marker is enabled
656 * @channel: channel name
657 * @name: marker name
658 *
659 * Returns 1 if the marker is enabled, 0 if disabled.
660 */
661 int is_marker_enabled(const char *channel, const char *name)
662 {
663 struct marker_entry *entry;
664
665 pthread_mutex_lock(&markers_mutex);
666 entry = get_marker(channel, name);
667 pthread_mutex_unlock(&markers_mutex);
668
669 return entry && !!entry->refcount;
670 }
671
672 /**
673 * marker_update_probe_range - Update a probe range
674 * @begin: beginning of the range
675 * @end: end of the range
676 *
677 * Updates the probe callback corresponding to a range of markers.
678 */
679 void marker_update_probe_range(struct marker *begin,
680 struct marker *end)
681 {
682 struct marker *iter;
683 struct marker_entry *mark_entry;
684
685 pthread_mutex_lock(&markers_mutex);
686 for (iter = begin; iter < end; iter++) {
687 mark_entry = get_marker(iter->channel, iter->name);
688 if (mark_entry) {
689 set_marker(mark_entry, iter, !!mark_entry->refcount);
690 /*
691 * ignore error, continue
692 */
693
694 /* This is added for UST. We emit a core_marker_id event
695 * for markers that are already registered to a probe
696 * upon library load. Otherwise, no core_marker_id will
697 * be generated for these markers. Is this the right thing
698 * to do?
699 */
700 trace_mark(metadata, core_marker_id,
701 "channel %s name %s event_id %hu "
702 "int #1u%zu long #1u%zu pointer #1u%zu "
703 "size_t #1u%zu alignment #1u%u",
704 iter->channel, iter->name, mark_entry->event_id,
705 sizeof(int), sizeof(long), sizeof(void *),
706 sizeof(size_t), ltt_get_alignment());
707 } else {
708 disable_marker(iter);
709 }
710 }
711 pthread_mutex_unlock(&markers_mutex);
712 }
713
714 static void lib_update_markers(void)
715 {
716 struct lib *lib;
717
718 /* FIXME: we should probably take a mutex here on libs */
719 //ust// pthread_mutex_lock(&module_mutex);
720 cds_list_for_each_entry(lib, &libs, list)
721 marker_update_probe_range(lib->markers_start,
722 lib->markers_start + lib->markers_count);
723 //ust// pthread_mutex_unlock(&module_mutex);
724 }
725
726 /*
727 * Update probes, removing the faulty probes.
728 *
729 * Internal callback only changed before the first probe is connected to it.
730 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
731 * transitions. All other transitions will leave the old private data valid.
732 * This makes the non-atomicity of the callback/private data updates valid.
733 *
734 * "special case" updates :
735 * 0 -> 1 callback
736 * 1 -> 0 callback
737 * 1 -> 2 callbacks
738 * 2 -> 1 callbacks
739 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
740 * Site effect : marker_set_format may delete the marker entry (creating a
741 * replacement).
742 */
743 static void marker_update_probes(void)
744 {
745 /* Core kernel markers */
746 //ust// marker_update_probe_range(__start___markers, __stop___markers);
747 /* Markers in modules. */
748 //ust// module_update_markers();
749 lib_update_markers();
750 tracepoint_probe_update_all();
751 /* Update immediate values */
752 core_imv_update();
753 //ust// module_imv_update(); /* FIXME: need to port for libs? */
754 marker_update_processes();
755 }
756
757 /**
758 * marker_probe_register - Connect a probe to a marker
759 * @channel: marker channel
760 * @name: marker name
761 * @format: format string
762 * @probe: probe handler
763 * @probe_private: probe private data
764 *
765 * private data must be a valid allocated memory address, or NULL.
766 * Returns 0 if ok, error value on error.
767 * The probe address must at least be aligned on the architecture pointer size.
768 */
769 int marker_probe_register(const char *channel, const char *name,
770 const char *format, marker_probe_func *probe,
771 void *probe_private)
772 {
773 struct marker_entry *entry;
774 int ret = 0, ret_err;
775 struct marker_probe_closure *old;
776 int first_probe = 0;
777
778 pthread_mutex_lock(&markers_mutex);
779 entry = get_marker(channel, name);
780 if (!entry) {
781 first_probe = 1;
782 entry = add_marker(channel, name, format);
783 if (IS_ERR(entry))
784 ret = PTR_ERR(entry);
785 if (ret)
786 goto end;
787 ret = ltt_channels_register(channel);
788 if (ret)
789 goto error_remove_marker;
790 ret = ltt_channels_get_index_from_name(channel);
791 if (ret < 0)
792 goto error_unregister_channel;
793 entry->channel_id = ret;
794 ret = ltt_channels_get_event_id(channel, name);
795 if (ret < 0)
796 goto error_unregister_channel;
797 entry->event_id = ret;
798 ret = 0;
799 trace_mark(metadata, core_marker_id,
800 "channel %s name %s event_id %hu "
801 "int #1u%zu long #1u%zu pointer #1u%zu "
802 "size_t #1u%zu alignment #1u%u",
803 channel, name, entry->event_id,
804 sizeof(int), sizeof(long), sizeof(void *),
805 sizeof(size_t), ltt_get_alignment());
806 } else if (format) {
807 if (!entry->format)
808 ret = marker_set_format(entry, format);
809 else if (strcmp(entry->format, format))
810 ret = -EPERM;
811 if (ret)
812 goto end;
813 }
814
815 /*
816 * If we detect that a call_rcu is pending for this marker,
817 * make sure it's executed now.
818 */
819 //ust// if (entry->rcu_pending)
820 //ust// rcu_cmm_barrier_sched();
821 old = marker_entry_add_probe(entry, probe, probe_private);
822 if (IS_ERR(old)) {
823 ret = PTR_ERR(old);
824 if (first_probe)
825 goto error_unregister_channel;
826 else
827 goto end;
828 }
829 pthread_mutex_unlock(&markers_mutex);
830
831 /* Activate marker if necessary */
832 marker_update_probes();
833
834 pthread_mutex_lock(&markers_mutex);
835 entry = get_marker(channel, name);
836 if (!entry)
837 goto end;
838 //ust// if (entry->rcu_pending)
839 //ust// rcu_cmm_barrier_sched();
840 entry->oldptr = old;
841 entry->rcu_pending = 1;
842 /* write rcu_pending before calling the RCU callback */
843 cmm_smp_wmb();
844 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
845 synchronize_rcu(); free_old_closure(&entry->rcu);
846 goto end;
847
848 error_unregister_channel:
849 ret_err = ltt_channels_unregister(channel);
850 WARN_ON(ret_err);
851 error_remove_marker:
852 ret_err = remove_marker(channel, name);
853 WARN_ON(ret_err);
854 end:
855 pthread_mutex_unlock(&markers_mutex);
856 return ret;
857 }
858 //ust// EXPORT_SYMBOL_GPL(marker_probe_register);
859
860 /**
861 * marker_probe_unregister - Disconnect a probe from a marker
862 * @channel: marker channel
863 * @name: marker name
864 * @probe: probe function pointer
865 * @probe_private: probe private data
866 *
867 * Returns the private data given to marker_probe_register, or an ERR_PTR().
868 * We do not need to call a synchronize_sched to make sure the probes have
869 * finished running before doing a module unload, because the module unload
870 * itself uses stop_machine(), which insures that every preempt disabled section
871 * have finished.
872 */
873 int marker_probe_unregister(const char *channel, const char *name,
874 marker_probe_func *probe, void *probe_private)
875 {
876 struct marker_entry *entry;
877 struct marker_probe_closure *old;
878 int ret = -ENOENT;
879
880 pthread_mutex_lock(&markers_mutex);
881 entry = get_marker(channel, name);
882 if (!entry)
883 goto end;
884 //ust// if (entry->rcu_pending)
885 //ust// rcu_cmm_barrier_sched();
886 old = marker_entry_remove_probe(entry, probe, probe_private);
887 pthread_mutex_unlock(&markers_mutex);
888
889 marker_update_probes();
890
891 pthread_mutex_lock(&markers_mutex);
892 entry = get_marker(channel, name);
893 if (!entry)
894 goto end;
895 //ust// if (entry->rcu_pending)
896 //ust// rcu_cmm_barrier_sched();
897 entry->oldptr = old;
898 entry->rcu_pending = 1;
899 /* write rcu_pending before calling the RCU callback */
900 cmm_smp_wmb();
901 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
902 synchronize_rcu(); free_old_closure(&entry->rcu);
903 remove_marker(channel, name); /* Ignore busy error message */
904 ret = 0;
905 end:
906 pthread_mutex_unlock(&markers_mutex);
907 return ret;
908 }
909 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister);
910
911 static struct marker_entry *
912 get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
913 {
914 struct marker_entry *entry;
915 unsigned int i;
916 struct cds_hlist_head *head;
917 struct cds_hlist_node *node;
918
919 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
920 head = &marker_table[i];
921 cds_hlist_for_each_entry(entry, node, head, hlist) {
922 if (!entry->ptype) {
923 if (entry->single.func == probe
924 && entry->single.probe_private
925 == probe_private)
926 return entry;
927 } else {
928 struct marker_probe_closure *closure;
929 closure = entry->multi;
930 for (i = 0; closure[i].func; i++) {
931 if (closure[i].func == probe &&
932 closure[i].probe_private
933 == probe_private)
934 return entry;
935 }
936 }
937 }
938 }
939 return NULL;
940 }
941
942 /**
943 * marker_probe_unregister_private_data - Disconnect a probe from a marker
944 * @probe: probe function
945 * @probe_private: probe private data
946 *
947 * Unregister a probe by providing the registered private data.
948 * Only removes the first marker found in hash table.
949 * Return 0 on success or error value.
950 * We do not need to call a synchronize_sched to make sure the probes have
951 * finished running before doing a module unload, because the module unload
952 * itself uses stop_machine(), which insures that every preempt disabled section
953 * have finished.
954 */
955 int marker_probe_unregister_private_data(marker_probe_func *probe,
956 void *probe_private)
957 {
958 struct marker_entry *entry;
959 int ret = 0;
960 struct marker_probe_closure *old;
961 char *channel = NULL, *name = NULL;
962
963 pthread_mutex_lock(&markers_mutex);
964 entry = get_marker_from_private_data(probe, probe_private);
965 if (!entry) {
966 ret = -ENOENT;
967 goto end;
968 }
969 //ust// if (entry->rcu_pending)
970 //ust// rcu_cmm_barrier_sched();
971 old = marker_entry_remove_probe(entry, NULL, probe_private);
972 channel = strdup(entry->channel);
973 name = strdup(entry->name);
974 pthread_mutex_unlock(&markers_mutex);
975
976 marker_update_probes();
977
978 pthread_mutex_lock(&markers_mutex);
979 entry = get_marker(channel, name);
980 if (!entry)
981 goto end;
982 //ust// if (entry->rcu_pending)
983 //ust// rcu_cmm_barrier_sched();
984 entry->oldptr = old;
985 entry->rcu_pending = 1;
986 /* write rcu_pending before calling the RCU callback */
987 cmm_smp_wmb();
988 //ust// call_rcu_sched(&entry->rcu, free_old_closure);
989 synchronize_rcu(); free_old_closure(&entry->rcu);
990 /* Ignore busy error message */
991 remove_marker(channel, name);
992 end:
993 pthread_mutex_unlock(&markers_mutex);
994 free(channel);
995 free(name);
996 return ret;
997 }
998 //ust// EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
999
1000 /**
1001 * marker_get_private_data - Get a marker's probe private data
1002 * @channel: marker channel
1003 * @name: marker name
1004 * @probe: probe to match
1005 * @num: get the nth matching probe's private data
1006 *
1007 * Returns the nth private data pointer (starting from 0) matching, or an
1008 * ERR_PTR.
1009 * Returns the private data pointer, or an ERR_PTR.
1010 * The private data pointer should _only_ be dereferenced if the caller is the
1011 * owner of the data, or its content could vanish. This is mostly used to
1012 * confirm that a caller is the owner of a registered probe.
1013 */
1014 void *marker_get_private_data(const char *channel, const char *name,
1015 marker_probe_func *probe, int num)
1016 {
1017 struct cds_hlist_head *head;
1018 struct cds_hlist_node *node;
1019 struct marker_entry *e;
1020 size_t channel_len = strlen(channel) + 1;
1021 size_t name_len = strlen(name) + 1;
1022 int i;
1023 u32 hash;
1024
1025 hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0);
1026 head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
1027 cds_hlist_for_each_entry(e, node, head, hlist) {
1028 if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) {
1029 if (!e->ptype) {
1030 if (num == 0 && e->single.func == probe)
1031 return e->single.probe_private;
1032 } else {
1033 struct marker_probe_closure *closure;
1034 int match = 0;
1035 closure = e->multi;
1036 for (i = 0; closure[i].func; i++) {
1037 if (closure[i].func != probe)
1038 continue;
1039 if (match++ == num)
1040 return closure[i].probe_private;
1041 }
1042 }
1043 break;
1044 }
1045 }
1046 return ERR_PTR(-ENOENT);
1047 }
1048 //ust// EXPORT_SYMBOL_GPL(marker_get_private_data);
1049
1050 /**
1051 * markers_compact_event_ids - Compact markers event IDs and reassign channels
1052 *
1053 * Called when no channel users are active by the channel infrastructure.
1054 * Called with lock_markers() and channel mutex held.
1055 */
1056 //ust// void markers_compact_event_ids(void)
1057 //ust// {
1058 //ust// struct marker_entry *entry;
1059 //ust// unsigned int i;
1060 //ust// struct hlist_head *head;
1061 //ust// struct hlist_node *node;
1062 //ust// int ret;
1063 //ust//
1064 //ust// for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1065 //ust// head = &marker_table[i];
1066 //ust// hlist_for_each_entry(entry, node, head, hlist) {
1067 //ust// ret = ltt_channels_get_index_from_name(entry->channel);
1068 //ust// WARN_ON(ret < 0);
1069 //ust// entry->channel_id = ret;
1070 //ust// ret = _ltt_channels_get_event_id(entry->channel,
1071 //ust// entry->name);
1072 //ust// WARN_ON(ret < 0);
1073 //ust// entry->event_id = ret;
1074 //ust// }
1075 //ust// }
1076 //ust// }
1077
1078 //ust//#ifdef CONFIG_MODULES
1079
1080 /*
1081 * Returns 0 if current not found.
1082 * Returns 1 if current found.
1083 */
1084 int lib_get_iter_markers(struct marker_iter *iter)
1085 {
1086 struct lib *iter_lib;
1087 int found = 0;
1088
1089 //ust// pthread_mutex_lock(&module_mutex);
1090 cds_list_for_each_entry(iter_lib, &libs, list) {
1091 if (iter_lib < iter->lib)
1092 continue;
1093 else if (iter_lib > iter->lib)
1094 iter->marker = NULL;
1095 found = marker_get_iter_range(&iter->marker,
1096 iter_lib->markers_start,
1097 iter_lib->markers_start + iter_lib->markers_count);
1098 if (found) {
1099 iter->lib = iter_lib;
1100 break;
1101 }
1102 }
1103 //ust// pthread_mutex_unlock(&module_mutex);
1104 return found;
1105 }
1106
1107 /**
1108 * marker_get_iter_range - Get a next marker iterator given a range.
1109 * @marker: current markers (in), next marker (out)
1110 * @begin: beginning of the range
1111 * @end: end of the range
1112 *
1113 * Returns whether a next marker has been found (1) or not (0).
1114 * Will return the first marker in the range if the input marker is NULL.
1115 */
1116 int marker_get_iter_range(struct marker **marker, struct marker *begin,
1117 struct marker *end)
1118 {
1119 if (!*marker && begin != end) {
1120 *marker = begin;
1121 return 1;
1122 }
1123 if (*marker >= begin && *marker < end)
1124 return 1;
1125 return 0;
1126 }
1127 //ust// EXPORT_SYMBOL_GPL(marker_get_iter_range);
1128
1129 static void marker_get_iter(struct marker_iter *iter)
1130 {
1131 int found = 0;
1132
1133 /* Core kernel markers */
1134 if (!iter->lib) {
1135 /* ust FIXME: how come we cannot disable the following line? we shouldn't need core stuff */
1136 found = marker_get_iter_range(&iter->marker,
1137 __start___markers, __stop___markers);
1138 if (found)
1139 goto end;
1140 }
1141 /* Markers in modules. */
1142 found = lib_get_iter_markers(iter);
1143 end:
1144 if (!found)
1145 marker_iter_reset(iter);
1146 }
1147
1148 void marker_iter_start(struct marker_iter *iter)
1149 {
1150 marker_get_iter(iter);
1151 }
1152 //ust// EXPORT_SYMBOL_GPL(marker_iter_start);
1153
1154 void marker_iter_next(struct marker_iter *iter)
1155 {
1156 iter->marker++;
1157 /*
1158 * iter->marker may be invalid because we blindly incremented it.
1159 * Make sure it is valid by marshalling on the markers, getting the
1160 * markers from following modules if necessary.
1161 */
1162 marker_get_iter(iter);
1163 }
1164 //ust// EXPORT_SYMBOL_GPL(marker_iter_next);
1165
1166 void marker_iter_stop(struct marker_iter *iter)
1167 {
1168 }
1169 //ust// EXPORT_SYMBOL_GPL(marker_iter_stop);
1170
1171 void marker_iter_reset(struct marker_iter *iter)
1172 {
1173 iter->lib = NULL;
1174 iter->marker = NULL;
1175 }
1176 //ust// EXPORT_SYMBOL_GPL(marker_iter_reset);
1177
1178 #ifdef CONFIG_MARKERS_USERSPACE
1179 /*
1180 * must be called with current->user_markers_mutex held
1181 */
1182 static void free_user_marker(char __user *state, struct cds_hlist_head *head)
1183 {
1184 struct user_marker *umark;
1185 struct cds_hlist_node *pos, *n;
1186
1187 cds_hlist_for_each_entry_safe(umark, pos, n, head, hlist) {
1188 if (umark->state == state) {
1189 cds_hlist_del(&umark->hlist);
1190 free(umark);
1191 }
1192 }
1193 }
1194
1195 /*
1196 * Update current process.
1197 * Note that we have to wait a whole scheduler period before we are sure that
1198 * every running userspace threads have their markers updated.
1199 * (synchronize_sched() can be used to insure this).
1200 */
1201 //ust// void marker_update_process(void)
1202 //ust// {
1203 //ust// struct user_marker *umark;
1204 //ust// struct hlist_node *pos;
1205 //ust// struct marker_entry *entry;
1206 //ust//
1207 //ust// pthread_mutex_lock(&markers_mutex);
1208 //ust// pthread_mutex_lock(&current->group_leader->user_markers_mutex);
1209 //ust// if (strcmp(current->comm, "testprog") == 0)
1210 //ust// DBG("do update pending for testprog");
1211 //ust// hlist_for_each_entry(umark, pos,
1212 //ust// &current->group_leader->user_markers, hlist) {
1213 //ust// DBG("Updating marker %s in %s", umark->name, current->comm);
1214 //ust// entry = get_marker("userspace", umark->name);
1215 //ust// if (entry) {
1216 //ust// if (entry->format &&
1217 //ust// strcmp(entry->format, umark->format) != 0) {
1218 //ust// WARN("error, wrong format in process %s",
1219 //ust// current->comm);
1220 //ust// break;
1221 //ust// }
1222 //ust// if (put_user(!!entry->refcount, umark->state)) {
1223 //ust// WARN("Marker in %s caused a fault",
1224 //ust// current->comm);
1225 //ust// break;
1226 //ust// }
1227 //ust// } else {
1228 //ust// if (put_user(0, umark->state)) {
1229 //ust// WARN("Marker in %s caused a fault", current->comm);
1230 //ust// break;
1231 //ust// }
1232 //ust// }
1233 //ust// }
1234 //ust// clear_thread_flag(TIF_MARKER_PENDING);
1235 //ust// pthread_mutex_unlock(&current->group_leader->user_markers_mutex);
1236 //ust// pthread_mutex_unlock(&markers_mutex);
1237 //ust// }
1238
1239 /*
1240 * Called at process exit and upon do_execve().
1241 * We assume that when the leader exits, no more references can be done to the
1242 * leader structure by the other threads.
1243 */
1244 void exit_user_markers(struct task_struct *p)
1245 {
1246 struct user_marker *umark;
1247 struct cds_hlist_node *pos, *n;
1248
1249 if (thread_group_leader(p)) {
1250 pthread_mutex_lock(&markers_mutex);
1251 pthread_mutex_lock(&p->user_markers_mutex);
1252 cds_hlist_for_each_entry_safe(umark, pos, n, &p->user_markers,
1253 hlist)
1254 free(umark);
1255 INIT_HLIST_HEAD(&p->user_markers);
1256 p->user_markers_sequence++;
1257 pthread_mutex_unlock(&p->user_markers_mutex);
1258 pthread_mutex_unlock(&markers_mutex);
1259 }
1260 }
1261
1262 int is_marker_enabled(const char *channel, const char *name)
1263 {
1264 struct marker_entry *entry;
1265
1266 pthread_mutex_lock(&markers_mutex);
1267 entry = get_marker(channel, name);
1268 pthread_mutex_unlock(&markers_mutex);
1269
1270 return entry && !!entry->refcount;
1271 }
1272 //ust// #endif
1273
1274 int marker_module_notify(struct notifier_block *self,
1275 unsigned long val, void *data)
1276 {
1277 struct module *mod = data;
1278
1279 switch (val) {
1280 case MODULE_STATE_COMING:
1281 marker_update_probe_range(mod->markers,
1282 mod->markers + mod->num_markers);
1283 break;
1284 case MODULE_STATE_GOING:
1285 marker_update_probe_range(mod->markers,
1286 mod->markers + mod->num_markers);
1287 break;
1288 }
1289 return 0;
1290 }
1291
1292 struct notifier_block marker_module_nb = {
1293 .notifier_call = marker_module_notify,
1294 .priority = 0,
1295 };
1296
1297 //ust// static int init_markers(void)
1298 //ust// {
1299 //ust// return register_module_notifier(&marker_module_nb);
1300 //ust// }
1301 //ust// __initcall(init_markers);
1302 /* TODO: call marker_module_nb() when a library is linked at runtime (dlopen)? */
1303
1304 #endif /* CONFIG_MODULES */
1305
1306 void ltt_dump_marker_state(struct ust_trace *trace)
1307 {
1308 struct marker_entry *entry;
1309 struct ltt_probe_private_data call_data;
1310 struct cds_hlist_head *head;
1311 struct cds_hlist_node *node;
1312 unsigned int i;
1313
1314 pthread_mutex_lock(&markers_mutex);
1315 call_data.trace = trace;
1316 call_data.serializer = NULL;
1317
1318 for (i = 0; i < MARKER_TABLE_SIZE; i++) {
1319 head = &marker_table[i];
1320 cds_hlist_for_each_entry(entry, node, head, hlist) {
1321 __trace_mark(0, metadata, core_marker_id,
1322 &call_data,
1323 "channel %s name %s event_id %hu "
1324 "int #1u%zu long #1u%zu pointer #1u%zu "
1325 "size_t #1u%zu alignment #1u%u",
1326 entry->channel,
1327 entry->name,
1328 entry->event_id,
1329 sizeof(int), sizeof(long),
1330 sizeof(void *), sizeof(size_t),
1331 ltt_get_alignment());
1332 if (entry->format)
1333 __trace_mark(0, metadata,
1334 core_marker_format,
1335 &call_data,
1336 "channel %s name %s format %s",
1337 entry->channel,
1338 entry->name,
1339 entry->format);
1340 }
1341 }
1342 pthread_mutex_unlock(&markers_mutex);
1343 }
1344 //ust// EXPORT_SYMBOL_GPL(ltt_dump_marker_state);
1345
1346 static void (*new_marker_cb)(struct marker *) = NULL;
1347
1348 void marker_set_new_marker_cb(void (*cb)(struct marker *))
1349 {
1350 new_marker_cb = cb;
1351 }
1352
1353 static void new_markers(struct marker *start, struct marker *end)
1354 {
1355 if(new_marker_cb) {
1356 struct marker *m;
1357 for(m=start; m < end; m++) {
1358 new_marker_cb(m);
1359 }
1360 }
1361 }
1362
1363 int marker_register_lib(struct marker *markers_start, int markers_count)
1364 {
1365 struct lib *pl;
1366
1367 pl = (struct lib *) zmalloc(sizeof(struct lib));
1368
1369 pl->markers_start = markers_start;
1370 pl->markers_count = markers_count;
1371
1372 /* FIXME: maybe protect this with its own mutex? */
1373 lock_markers();
1374 cds_list_add(&pl->list, &libs);
1375 unlock_markers();
1376
1377 new_markers(markers_start, markers_start + markers_count);
1378
1379 /* FIXME: update just the loaded lib */
1380 lib_update_markers();
1381
1382 DBG("just registered a markers section from %p and having %d markers", markers_start, markers_count);
1383
1384 return 0;
1385 }
1386
1387 int marker_unregister_lib(struct marker *markers_start)
1388 {
1389 struct lib *lib;
1390
1391 /*FIXME: implement; but before implementing, marker_register_lib must
1392 have appropriate locking. */
1393
1394 lock_markers();
1395
1396 /* FIXME: we should probably take a mutex here on libs */
1397 //ust// pthread_mutex_lock(&module_mutex);
1398 cds_list_for_each_entry(lib, &libs, list) {
1399 if(lib->markers_start == markers_start) {
1400 struct lib *lib2free = lib;
1401 cds_list_del(&lib->list);
1402 free(lib2free);
1403 break;
1404 }
1405 }
1406
1407 unlock_markers();
1408
1409 return 0;
1410 }
1411
1412 static int initialized = 0;
1413
1414 void __attribute__((constructor)) init_markers(void)
1415 {
1416 if(!initialized) {
1417 marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker));
1418 initialized = 1;
1419 }
1420 }
1421
1422 void __attribute__((constructor)) destroy_markers(void)
1423 {
1424 marker_unregister_lib(__start___markers);
1425 }
This page took 0.057284 seconds and 4 git commands to generate.