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