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